﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.IO;
using School.Entities;
using School.Common;
using System.Data.Entity;
using School.Admission;


namespace School.Pages.Admission
{
    public partial class AddStudent : System.Web.UI.Page
    {


        DataTable dt = null;
        DataTable dtmystudent = null;
        int i = 1;
        long? myhomeaddressid;
        long? myworkaddressid;

        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1004) == true)
                {
                    try
                    {

                        if (Request.QueryString.Get("sid").ToString() == "")
                        {
                            Response.Redirect("StudentSearch.aspx");
                        }
                        if (!Page.IsPostBack)
                        {
                            lblstudentid.Text = (Request.QueryString.Get("sid").ToString());
                            Session["dt"] = null;
                            lbStudentData.CssClass = "tabopend";
                            Fillcrls();
                            InitializeControls();
                            updateImageUrl();
                            SelectMyStudent();
                            tabstudenthandel();
                        }
                    }
                    catch
                    {
                        return;
                    }
                }

                else

                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
            Response.Redirect("../organization/no_privelege.aspx");
        }
        # region "  ****************** Tabs Handels ***********************"
        protected void lbStudentData_Click(object sender, EventArgs e)
        {
            tabstudenthandel();
            updateImageUrl();
        }

        protected void tabstudenthandel()
        {

            lbFatherData.CssClass = "tabclosed";
            lbMotherData.CssClass = "tabclosed";
            lbCustodyData.CssClass = "tabclosed";
            lbXSchoolData.CssClass = "tabclosed";
            lbRelativesData.CssClass = "tabclosed";
            lbStudentQualificatioSkillData.CssClass = "tabclosed";
            lbEmergencyData.CssClass = "tabclosed";

            lbStudentData.CssClass = "tabopend";

            pnl1.Visible = true;
            pnl2.Visible = false;
            pnl3.Visible = false;
            pnl4.Visible = false;
            pnl5.Visible = false;
            pnl6.Visible = false;
            pnl7.Visible = false;
            pnl8.Visible = false;
            updateImageUrl();
        }
        protected void lbFatherData_Click(object sender, EventArgs e)
        {
            lbFatherData.CssClass = "tabopend";

            lbMotherData.CssClass = "tabclosed";
            lbCustodyData.CssClass = "tabclosed";
            lbXSchoolData.CssClass = "tabclosed";
            lbRelativesData.CssClass = "tabclosed";
            lbStudentQualificatioSkillData.CssClass = "tabclosed";
            lbEmergencyData.CssClass = "tabclosed";
            lbStudentData.CssClass = "tabclosed";

            pnl1.Visible = false;
            pnl2.Visible = true;
            pnl3.Visible = false;
            pnl4.Visible = false;
            pnl5.Visible = false;
            pnl6.Visible = false;
            pnl7.Visible = false;
            pnl8.Visible = false;
            updateImageUrl();
        }

        protected void lbMotherData_Click(object sender, EventArgs e)
        {



            lbMotherData.CssClass = "tabopend";

            lbFatherData.CssClass = "tabclosed";
            lbCustodyData.CssClass = "tabclosed";
            lbXSchoolData.CssClass = "tabclosed";
            lbRelativesData.CssClass = "tabclosed";
            lbStudentQualificatioSkillData.CssClass = "tabclosed";
            lbEmergencyData.CssClass = "tabclosed";
            lbStudentData.CssClass = "tabclosed";

            pnl1.Visible = false;
            pnl2.Visible = false;
            pnl3.Visible = true;
            pnl4.Visible = false;
            pnl5.Visible = false;
            pnl6.Visible = false;
            pnl7.Visible = false;
            pnl8.Visible = false;
            updateImageUrl();
        }

        protected void lbCustodyData_Click(object sender, EventArgs e)
        {
            lbCustodyData.CssClass = "tabopend";

            lbMotherData.CssClass = "tabclosed";
            lbFatherData.CssClass = "tabclosed";
            lbXSchoolData.CssClass = "tabclosed";
            lbRelativesData.CssClass = "tabclosed";
            lbStudentQualificatioSkillData.CssClass = "tabclosed";
            lbEmergencyData.CssClass = "tabclosed";
            lbStudentData.CssClass = "tabclosed";

            pnl1.Visible = false;
            pnl2.Visible = false;
            pnl3.Visible = false;
            pnl4.Visible = true;
            pnl5.Visible = false;
            pnl6.Visible = false;
            pnl7.Visible = false;
            pnl8.Visible = false;
            updateImageUrl();
        }

        protected void lbXSchoolData_Click(object sender, EventArgs e)
        {
            lbXSchoolData.CssClass = "tabopend";


            lbCustodyData.CssClass = "tabclosed";
            lbMotherData.CssClass = "tabclosed";
            lbFatherData.CssClass = "tabclosed";
            lbRelativesData.CssClass = "tabclosed";
            lbStudentQualificatioSkillData.CssClass = "tabclosed";
            lbEmergencyData.CssClass = "tabclosed";
            lbStudentData.CssClass = "tabclosed";

            pnl1.Visible = false;
            pnl2.Visible = false;
            pnl3.Visible = false;
            pnl4.Visible = false;
            pnl5.Visible = true;
            pnl6.Visible = false;
            pnl7.Visible = false;
            pnl8.Visible = false;
            updateImageUrl();
        }

        protected void lbRelativesData_Click(object sender, EventArgs e)
        {
            
            Session["FIdentification_Number"] = txtFIdentification_Number.Text.Trim();

            lbRelativesData.CssClass = "tabopend";


            lbXSchoolData.CssClass = "tabclosed";
            lbCustodyData.CssClass = "tabclosed";
            lbMotherData.CssClass = "tabclosed";
            lbFatherData.CssClass = "tabclosed";
            lbStudentQualificatioSkillData.CssClass = "tabclosed";
            lbEmergencyData.CssClass = "tabclosed";
            lbStudentData.CssClass = "tabclosed";

            pnl1.Visible = false;
            pnl2.Visible = false;
            pnl3.Visible = false;
            pnl4.Visible = false;
            pnl5.Visible = false;
            pnl6.Visible = true;
            pnl7.Visible = false;
            pnl8.Visible = false;
            updateImageUrl();
        }

        protected void lbStudentQualificatioSkillData_Click(object sender, EventArgs e)
        {
            lbStudentQualificatioSkillData.CssClass = "tabopend";

            lbRelativesData.CssClass = "tabclosed";
            lbXSchoolData.CssClass = "tabclosed";
            lbCustodyData.CssClass = "tabclosed";
            lbMotherData.CssClass = "tabclosed";
            lbFatherData.CssClass = "tabclosed";
            lbEmergencyData.CssClass = "tabclosed";
            lbStudentData.CssClass = "tabclosed";

            pnl1.Visible = false;
            pnl2.Visible = false;
            pnl3.Visible = false;
            pnl4.Visible = false;
            pnl5.Visible = false;
            pnl6.Visible = false;
            pnl7.Visible = true;
            pnl8.Visible = false;
            updateImageUrl();
        }

        protected void lbEmergencyData_Click(object sender, EventArgs e)
        {
            lbEmergencyData.CssClass = "tabopend";


            lbStudentQualificatioSkillData.CssClass = "tabclosed";
            lbRelativesData.CssClass = "tabclosed";
            lbXSchoolData.CssClass = "tabclosed";
            lbCustodyData.CssClass = "tabclosed";
            lbMotherData.CssClass = "tabclosed";
            lbFatherData.CssClass = "tabclosed";
            lbStudentData.CssClass = "tabclosed";

            pnl1.Visible = false;
            pnl2.Visible = false;
            pnl3.Visible = false;
            pnl4.Visible = false;
            pnl5.Visible = false;
            pnl6.Visible = false;
            pnl7.Visible = false;
            pnl8.Visible = true;
            updateImageUrl();
        }

        # endregion


        # region "Fill all crls"
        protected void Fillcrls()
        {
            fillcrls aa = new fillcrls();
            aa.fillddlcrls(ddlFGreggorian_GraduationDate);
            aa.fillddlcrls(ddlFHijri_GraduationDate);
            aa.fillddlcrls(ddlMGreggorian_GraduationDate);
            aa.fillddlcrls(ddlMHijri_GraduationDate);
            aa.fillddlcrls(ddlCGreggorian_GraduationDate);
            aa.fillddlcrls(ddlCHijri_GraduationDate);
            btnsearchrelative.Attributes.Add("onClick", "javascript:window.open('getrelative.aspx?crl1=" + txtrelativeresualt.ClientID + "&crl2=" + lblrelativeresualt.ClientID + "&stype=1' , true, 'height=800,width=800,status=no,toolbar=no,menubar=no,location=no')");
            btnaddrelative.Attributes.Add("onClick", "javascript:window.open('getrelative.aspx?crl1=" + lblmyrelstudentid.ClientID + "&crl2=" + lblmyrelstudentid.ClientID + "&stype=0' , true, 'height=800,width=800,status=no,toolbar=no,menubar=no,location=no')");
            Image1.Attributes.Add("onClick", "javascript:window.open('ImageUploadPage.aspx?crl1=" + lblstudentid.Text + "&crl2=" + Image1.ClientID + "' , true, 'height=500,width=500,status=yes,toolbar=yes,menubar=yes,location=no')");

            ddlGender_Id.DataSource = LookUps.Genders();
            ddlGender_Id.DataTextField = "Kind";
            ddlGender_Id.DataValueField = "Id";
            ddlGender_Id.DataBind();

            ddlReligion_Id.DataSource = LookUps.Religions();
            ddlReligion_Id.DataTextField = "Name";
            ddlReligion_Id.DataValueField = "Id";
            ddlReligion_Id.DataBind();

            lbCountry_Id.DataSource = LookUps.Countries();
            lbCountry_Id.DataTextField = "Name";
            lbCountry_Id.DataValueField = "Id";
            lbCountry_Id.DataBind();

            lbUsed_Home_Languages.DataSource = LookUps.Languages();
            lbUsed_Home_Languages.DataTextField = "Name";
            lbUsed_Home_Languages.DataValueField = "Name";
            lbUsed_Home_Languages.DataBind();

            ddlCampus.DataSource = Structure.GetCampuses();
            ddlCampus.DataTextField = "Name";
            ddlCampus.DataValueField = "Id";
            ddlCampus.DataBind();
            ddlCampus.Items.Insert(0, new ListItem("اختيار", "-1"));


            //*************************Father****************************
            lbFCountry_Id.DataSource = LookUps.Countries();
            lbFCountry_Id.DataTextField = "Name";
            lbFCountry_Id.DataValueField = "Id";
            lbFCountry_Id.DataBind();

            ddlFQualification_Id.DataSource = LookUps.Qualifications();
            ddlFQualification_Id.DataTextField = "Name";
            ddlFQualification_Id.DataValueField = "Id";
            ddlFQualification_Id.DataBind();

            ddlFCareer.DataSource = LookUps.Jobs();
            ddlFCareer.DataTextField = "Name";
            ddlFCareer.DataValueField = "Id";
            ddlFCareer.DataBind();


            ddlFStat_Id.DataSource = LookUps.State();
            ddlFStat_Id.DataTextField = "Name";
            ddlFStat_Id.DataValueField = "Id";
            ddlFStat_Id.DataBind();
            ddlFStat_Id.SelectedValue = "1";

            ddlFDistrict_Id.DataSource = LookUps.GetDestrictByState(int.Parse(ddlFStat_Id.SelectedValue));
            ddlFDistrict_Id.DataTextField = "Name";
            ddlFDistrict_Id.DataValueField = "Id";
            ddlFDistrict_Id.DataBind();

            ddlFhStat_Id.DataSource = LookUps.State();
            ddlFhStat_Id.DataTextField = "Name";
            ddlFhStat_Id.DataValueField = "Id";
            ddlFhStat_Id.DataBind();
            ddlFhStat_Id.SelectedValue = "1";


            ddlFhDistrict_Id.DataSource = LookUps.GetDestrictByState(int.Parse(ddlFhStat_Id.SelectedValue));
            ddlFhDistrict_Id.DataTextField = "Name";
            ddlFhDistrict_Id.DataValueField = "Id";
            ddlFhDistrict_Id.DataBind();


            //*************************Mother****************************

            lbMCountry_Id.DataSource = LookUps.Countries();
            lbMCountry_Id.DataTextField = "Name";
            lbMCountry_Id.DataValueField = "Id";
            lbMCountry_Id.DataBind();

            ddlMQualification_Id.DataSource = LookUps.Qualifications();
            ddlMQualification_Id.DataTextField = "Name";
            ddlMQualification_Id.DataValueField = "Id";
            ddlMQualification_Id.DataBind();

            ddlMCareer.DataSource = LookUps.Jobs();
            ddlMCareer.DataTextField = "Name";
            ddlMCareer.DataValueField = "Id";
            ddlMCareer.DataBind();

            ddlMStat_Id.DataSource = LookUps.State();
            ddlMStat_Id.DataTextField = "Name";
            ddlMStat_Id.DataValueField = "Id";
            ddlMStat_Id.DataBind();
            ddlMStat_Id.SelectedValue = "1";


            ddlMDistrict_Id.DataSource = LookUps.GetDestrictByState(int.Parse(ddlMStat_Id.SelectedValue));
            ddlMDistrict_Id.DataTextField = "Name";
            ddlMDistrict_Id.DataValueField = "Id";
            ddlMDistrict_Id.DataBind();

            ddlMhStat_Id.DataSource = LookUps.State();
            ddlMhStat_Id.DataTextField = "Name";
            ddlMhStat_Id.DataValueField = "Id";
            ddlMhStat_Id.DataBind();
            ddlMhStat_Id.SelectedValue = "1";


            ddlMhDistrict_Id.DataSource = LookUps.GetDestrictByState(int.Parse(ddlMhStat_Id.SelectedValue));
            ddlMhDistrict_Id.DataTextField = "Name";
            ddlMhDistrict_Id.DataValueField = "Id";
            ddlMhDistrict_Id.DataBind();

            /////////******************** Custody *******************************


            lbCCountry_Id.DataSource = LookUps.Countries();
            lbCCountry_Id.DataTextField = "Name";
            lbCCountry_Id.DataValueField = "Id";
            lbCCountry_Id.DataBind();

            ddlCQualification_Id.DataSource = LookUps.Qualifications();
            ddlCQualification_Id.DataTextField = "Name";
            ddlCQualification_Id.DataValueField = "Id";
            ddlCQualification_Id.DataBind();

            ddlCCareer.DataSource = LookUps.Jobs();
            ddlCCareer.DataTextField = "Name";
            ddlCCareer.DataValueField = "Id";
            ddlCCareer.DataBind();

            ddlCStat_Id.DataSource = LookUps.State();
            ddlCStat_Id.DataTextField = "Name";
            ddlCStat_Id.DataValueField = "Id";
            ddlCStat_Id.DataBind();
            ddlCStat_Id.SelectedValue = "1";


            ddlCDistrict_Id.DataSource = LookUps.GetDestrictByState(int.Parse(ddlCStat_Id.SelectedValue));
            ddlCDistrict_Id.DataTextField = "Name";
            ddlCDistrict_Id.DataValueField = "Id";
            ddlCDistrict_Id.DataBind();

            ddlChStat_Id.DataSource = LookUps.State();
            ddlChStat_Id.DataTextField = "Name";
            ddlChStat_Id.DataValueField = "Id";
            ddlChStat_Id.DataBind();
            ddlChStat_Id.SelectedValue = "1";


            ddlChDistrict_Id.DataSource = LookUps.GetDestrictByState(int.Parse(ddlChStat_Id.SelectedValue));
            ddlChDistrict_Id.DataTextField = "Name";
            ddlChDistrict_Id.DataValueField = "Id";
            ddlChDistrict_Id.DataBind();

            lbParentsSocialStatus_Id.DataSource = LookUps.ParentsSocialStatus();
            lbParentsSocialStatus_Id.DataTextField = "Name";
            lbParentsSocialStatus_Id.DataValueField = "Id";
            lbParentsSocialStatus_Id.DataBind();


            lbCustodyType_Id.DataSource = LookUps.CustodyType();
            lbCustodyType_Id.DataTextField = "Name";
            lbCustodyType_Id.DataValueField = "Id";
            lbCustodyType_Id.DataBind();


            lbStudentLivingPlace_Id.DataSource = LookUps.LivingWith();
            lbStudentLivingPlace_Id.DataTextField = "Name";
            lbStudentLivingPlace_Id.DataValueField = "Id";
            lbStudentLivingPlace_Id.DataBind();

            //***************************************** xschooles******************


            ddlCertification.DataSource = Structure.GetCertificates();
            ddlCertification.DataTextField = "Name";
            ddlCertification.DataValueField = "Id";
            ddlCertification.DataBind();
            ddlCertification.Items.Insert(0, new ListItem("اختيار", "-1"));


            ddlFirstLanguage.DataSource = LookUps.Languages();
            ddlFirstLanguage.DataTextField = "Name";
            ddlFirstLanguage.DataValueField = "Id";
            ddlFirstLanguage.DataBind();


            ddlSecondLanguage.DataSource = LookUps.Languages();
            ddlSecondLanguage.DataTextField = "Name";
            ddlSecondLanguage.DataValueField = "Id";
            ddlSecondLanguage.DataBind();


            //************************* Emergance ********************************

            QualificationSkillRepeater.DataSource = LookUps.QualificationSkill();
            QualificationSkillRepeater.DataBind();


        }


        //***************** This is function to initlaize crl  ***************************
        //-**************************************************************************
        protected void InitializeControls()
        {

            //*************************** student *****************************

            txtAr_FirstName.Text = "";
            txtAr_SecondName.Text = "";
            txtAr_ThirdNmae.Text = "";
            txtAr_FourthName.Text = "";
            txtStLoginName.Text = txtStLoginPassword.Text = string.Empty;

            txtEn_FirstName.Text = "";
            txtEn_SecondName.Text = "";
            txtEn_ThirdName.Text = "";
            txtEn_FourthName.Text = "";

            txtGregorian_BirthDate.Text = "";
            txtHijri_BirthDate.Text = "";
            ddlGender_Id.SelectedIndex = 0;
            ddlReligion_Id.SelectedIndex = 0;
            lbCountry_Id.SelectedIndex = 0;
            lbFCountry_Id.SelectedIndex = 0;
            lbMCountry_Id.SelectedIndex = 0;
            lbCCountry_Id.SelectedIndex = 0;
            txtID_Number.Text = "";
            txtID_Gregorian_Issue_Date.Text = "";
            txtID_Hijri_Issue_Date1.Text = "";
            txtPassport_Number.Text = "";
            txtPassport_Gregorian_Issue_Date.Text = "";
            txtPassport_Hijri_Issue_Date1.Text = "";
            ddlArrangement_bt_Brothers.SelectedIndex = 0;
            lbUsed_Home_Languages.SelectedIndex = 0;

            //*************************** Father *****************************
            txtFAr_FirstName.Text = "";
            txtFAr_SecondName.Text = "";
            txtFAr_ThirdNmae.Text = "";
            txtFAr_FourthName.Text = "";
            txtFLoginName.Text = txtFloginPassword.Text = string.Empty;

            txtFEn_FirstName.Text = "";
            txtFEn_SecondName.Text = "";
            txtFEn_ThirdName.Text = "";
            txtFEn_FourthName.Text = "";

            txtFGregorian_BirthDate.Text = "";
            txtFHijri_BirthDate.Text = "";
            txtFIdentification_Number.Text = "";
            txtFPasport_Number.Text = "";
            ddlFCareer.SelectedIndex = 0;
            txtFCompanyName.Text = "";
            txtFBuildingNo.Text = "";
            txtFFloorNo.Text = "";
            txtFFlatNo.Text = "";
            txtFStreetName.Text = "";

            txtFPostCode.Text = "";
            txtFPhone1.Text = "";
            txtFPhone2.Text = "";
            txtFPhone3.Text = "";
            txtFInternalPhoneExtension.Text = "";
            txtFFAX.Text = "";
            txtFMobile1.Text = "";
            txtFMobile2.Text = "";
            txtFEmail1.Text = "";
            txtFEmail2.Text = "";
            txtFhBuildingNo.Text = "";
            txtFhFloorNo.Text = "";
            txtFhFlatNo.Text = "";
            txtFhStreetName.Text = "";

            txtFhPostCode.Text = "";
            txtFhPhone1.Text = "";
            txtFhPhone2.Text = "";
            txtFhPhone3.Text = "";

            //*************************** Mother *****************************

            txtMAr_FirstName.Text = "";
            txtMAr_SecondName.Text = "";
            txtMAr_ThirdNmae.Text = "";
            txtMAr_FourthName.Text = "";
            txtMLoginName.Text = txtMLoginPassword.Text = string.Empty;

            txtMEn_FirstName.Text = "";
            txtMEn_SecondName.Text = "";
            txtMEn_ThirdName.Text = "";
            txtMEn_FourthName.Text = "";

            txtMGregorian_BirthDate.Text = "";
            txtMHijri_BirthDate.Text = "";
            txtMIdentification_Number.Text = "";
            txtMPasport_Number.Text = "";
            ddlMCareer.SelectedIndex = 0;
            txtMCompanyName.Text = "";
            txtMBuildingNo.Text = "";
            txtMFloorNo.Text = "";
            txtMFlatNo.Text = "";
            txtMStreetName.Text = "";

            txtMPostCode.Text = "";
            txtMPhone1.Text = "";
            txtMPhone2.Text = "";
            txtMPhone3.Text = "";
            txtMInternalPhoneExtension.Text = "";
            txtMFAX.Text = "";
            txtMMobile1.Text = "";
            txtMMobile2.Text = "";
            txtMEmail1.Text = "";
            txtMEmail2.Text = "";
            txtMhBuildingNo.Text = "";
            txtMhFloorNo.Text = "";
            txtMhFlatNo.Text = "";
            txtMhStreetName.Text = "";

            txtMhPostCode.Text = "";
            txtMhPhone1.Text = "";
            txtMhPhone2.Text = "";
            txtMhPhone3.Text = "";

            /////////******************** Custody *******************************
            lbParentsSocialStatus_Id.SelectedIndex = 0;
            lbCustodyType_Id.SelectedIndex = 0;
            lbStudentLivingPlace_Id.SelectedIndex = 0;
            txtCRelationToStudent.Text = "";

            txtCAr_FirstName.Text = "";
            txtCAr_SecondName.Text = "";
            txtCAr_ThirdNmae.Text = "";
            txtCAr_FourthName.Text = "";
            txtCEnLoginName.Text = txtCEnLoginPassword.Text = string.Empty;

            txtCEn_FirstName.Text = "";
            txtCEn_SecondName.Text = "";
            txtCEn_ThirdName.Text = "";
            txtCEn_FourthName.Text = "";

            txtCGregorian_BirthDate.Text = "";
            txtCHijri_BirthDate.Text = "";
            txtCIdentification_Number.Text = "";
            txtCPasport_Number.Text = "";
            ddlCCareer.SelectedIndex = 0;
            txtCCompanyName.Text = "";
            txtCBuildingNo.Text = "";
            txtCFloorNo.Text = "";
            txtCFlatNo.Text = "";
            txtCStreetName.Text = "";

            txtCPostCode.Text = "";
            txtCPhone1.Text = "";
            txtCPhone2.Text = "";
            txtCPhone3.Text = "";
            txtCInternalPhoneExtension.Text = "";
            txtCFAX.Text = "";
            txtCMobile1.Text = "";
            txtCMobile2.Text = "";
            txtCEmail1.Text = "";
            txtCEmail2.Text = "";
            txtChBuildingNo.Text = "";
            txtChFloorNo.Text = "";
            txtChFlatNo.Text = "";
            txtChStreetName.Text = "";

            txtChPostCode.Text = "";
            txtChPhone1.Text = "";
            txtChPhone2.Text = "";
            txtChPhone3.Text = "";

            clearmyschool();

            ///////**********************************************Emergancy

            txtName1.Text = "";
            txtName2.Text = "";
            txtName3.Text = "";
            txtName4.Text = "";
            txtName5.Text = "";

            txtPhone1.Text = "";
            txtPhone2.Text = "";
            txtPhone3.Text = "";
            txtPhone4.Text = "";
            txtPhone5.Text = "";

            txtMobilePhone1.Text = "";
            txtMobilePhone2.Text = "";
            txtMobilePhone3.Text = "";
            txtMobilePhone4.Text = "";
            txtMobilePhone5.Text = "";

            if (txtFileNumber.Text != "")
            {

                SelectMyStudent();
            }


        }

        protected void clearmyschool()
        {

            ////////************************** sxchools**********************
            txtSatrtDate.Text = "";
            txtEndDate.Text = "";
            txtSchoolName.Text = "";
            txtAddress.Text = "";
            ddlCertification.SelectedIndex = 0;
            ddlGrade.Items.Clear();
            ddlGrade.Enabled = false;

            ddlClass.Items.Clear();
            ddlClass.Enabled = false;

            ddlFirstLanguage.SelectedIndex = 0;
            ddlSecondLanguage.SelectedIndex = 0;

        }

        # endregion


        # region   "Execute commands sent to the form"

        protected void SelectMyStudent()
        {
            TE_Student student = School.Admission.Student.Single(long.Parse(Request.QueryString.Get("sid")));
            lblstudentid.Text = Convert.ToString(student.Id);
            txtRegistrationNumber.Text = Convert.ToString(student.RegistrationNumber);
            txtFileNumber.Text = Convert.ToString(student.FileNumber);
            txtStLoginName.Text = student.UserName;
            ddlCampus.SelectedValue = student.Campus_Id.ToString();
            if (ddlCampus.SelectedValue != "0")
            {
                ddlCertificate.Items.Clear();
                ddlCertificate.DataSource = Structure.GetCertificatesByCampus(long.Parse(ddlCampus.SelectedValue));
                ddlCertificate.DataTextField = "Name";
                ddlCertificate.DataValueField = "Id";
                ddlCertificate.DataBind();
            }

            ddlCertificate.SelectedValue = student.Certificate_Id.ToString();

            if (ddlCertificate.SelectedValue != "0")
            {
                ddlStadge.Items.Clear();
                ddlStadge.DataSource = Structure.GetStagesByCertificate(long.Parse(ddlCertificate.SelectedValue));
                ddlStadge.DataTextField = "Name";
                ddlStadge.DataValueField = "Id";
                ddlStadge.DataBind();
            }


            ddlStadge.SelectedValue = student.Stage_Id.ToString();

            if (ddlStadge.SelectedValue != "0")
            {
                ddltopgrad.Items.Clear();
                ddltopgrad.DataSource = Structure.GetGradesByStage(long.Parse(ddlStadge.SelectedValue));
                ddltopgrad.DataTextField = "Name";
                ddltopgrad.DataValueField = "Id";
                ddltopgrad.DataBind();
            }


            ddltopgrad.SelectedValue = student.Grade_ID.ToString();
            txtAr_FirstName.Text = student.Ar_FirstName;
            txtAr_SecondName.Text = student.Ar_SecondName;
            txtAr_ThirdNmae.Text = student.Ar_ThirdNmae;
            txtAr_FourthName.Text = student.Ar_FourthName;
            txtFAr_FirstName.Text = student.Ar_SecondName;
            txtFAr_SecondName.Text = student.Ar_ThirdNmae;
            txtFAr_ThirdNmae.Text = student.Ar_FourthName;
            txtEn_FirstName.Text = student.En_FirstName;
            txtEn_SecondName.Text = student.En_SecondName;
            txtEn_ThirdName.Text = student.En_ThirdName;
            txtEn_FourthName.Text = student.En_FourthName;
            txtStLoginName.Text = student.UserName;
            txtStLoginPassword.Text = student.Password;

            if (student.Gregorian_BirthDate != null)
            {
                txtGregorian_BirthDate.Text = DateTime.Parse(student.Gregorian_BirthDate.ToString()).ToShortDateString();

            }

            if (student.Hijri_BirthDate != null)
            {
                txtHijri_BirthDate.Text = DateTime.Parse(student.Hijri_BirthDate.ToString()).ToShortDateString();
            }

            if (student.Gender_Id != null)
            {
                ddlGender_Id.SelectedValue = student.Gender_Id.ToString();
            }

            if (student.Religion_Id != null)
            {
                ddlReligion_Id.SelectedValue = student.Religion_Id.ToString();
            }

            if (student.Country_Id != null)
            {
                lbCountry_Id.SelectedValue = student.Country_Id.ToString();
            }


            txtID_Number.Text = student.ID_Number;

            if (student.ID_Gregorian_Issue_Date != null)
            {
                txtID_Gregorian_Issue_Date.Text = DateTime.Parse(student.ID_Gregorian_Issue_Date.ToString()).ToShortDateString();
            }

            if (student.ID_Hijri_Issue_Date1 != null)
            {
                txtID_Hijri_Issue_Date1.Text = DateTime.Parse(student.ID_Hijri_Issue_Date1.ToString()).ToShortDateString();

            }

            txtPassport_Number.Text = student.Passport_Number;

            if (student.Passport_Gregorian_Issue_Date != null)
            {
                txtPassport_Gregorian_Issue_Date.Text = DateTime.Parse(student.Passport_Gregorian_Issue_Date.ToString()).ToShortDateString();
            }
            if (student.Passport_Hijri_Issue_Date1 != null)
            {
                txtPassport_Hijri_Issue_Date1.Text = DateTime.Parse(student.Passport_Hijri_Issue_Date1.ToString()).ToShortDateString();

            }
            if (student.Arrangement_bt_Brothers != null)
            {
                ddlArrangement_bt_Brothers.SelectedValue = student.Arrangement_bt_Brothers.ToString();
            }
            if (student.Used_Home_Languages != null)
            {
                lbUsed_Home_Languages.SelectedIndex = lbUsed_Home_Languages.Items.IndexOf(new ListItem(student.Used_Home_Languages, student.Used_Home_Languages));
            }

            // fill father fileds if father inserted ****************************************************************

            SD_Father myupdatefather = new SD_Father();
            long mystudentid = long.Parse(lblstudentid.Text);
            SD_Father myfather = Father.SingleByStudentId(mystudentid);


            if (myfather != null)
            {
                myupdatefather.Id = myfather.Id;
                txtFAr_FourthName.Text = myfather.Ar_FourthName;
                txtFEn_FirstName.Text = myfather.En_FirstName;
                txtFEn_SecondName.Text = myfather.En_SecondName;
                txtFEn_ThirdName.Text = myfather.En_ThirdName;
                txtFEn_FourthName.Text = myfather.En_FourthName;
                txtFLoginName.Text = myfather.UserName;
                txtFloginPassword.Text = myfather.Password;

                if (myfather.Gregorian_BirthDate != null)
                {
                    txtFGregorian_BirthDate.Text = myfather.Gregorian_BirthDate.ToString();
                }
                if (myfather.Hijri_BirthDate != null)
                {
                    txtFHijri_BirthDate.Text = myfather.Hijri_BirthDate.ToString();
                }
                txtFIdentification_Number.Text = myfather.Identification_Number;
                txtFPasport_Number.Text = myfather.Pasport_Number;
                if (myfather.Qualification_Id != null)
                {
                    ddlFQualification_Id.SelectedValue = myfather.Qualification_Id.ToString();
                }
                txtFSpecialization.Text = myfather.Specialization;
                txtFPointOfGraduation.Text = myfather.PointOfGraduation;
                if (myfather.Greggorian_GraduationDate != null)
                {
                    ddlFGreggorian_GraduationDate.SelectedItem.Text = myfather.Greggorian_GraduationDate.ToString();
                }
                if (myfather.Hijri_GraduationDate != null)
                {
                    ddlFHijri_GraduationDate.SelectedItem.Text = myfather.Hijri_GraduationDate.ToString();
                }
                if (myfather.Nationality_Id != null)
                {
                    lbFCountry_Id.SelectedValue = myfather.Nationality_Id.ToString();
                }
                if (myfather.HomeAddress_Id != null && myfather.HomeAddress_Id != 0)
                {

                    SD_HomeAddress myfatherhomeaddree = HomeAddress.Single((long)(myfather.HomeAddress_Id));

                    if (myfatherhomeaddree.Id != null || myfatherhomeaddree.Id != 0)
                    {
                        txtFhBuildingNo.Text = myfatherhomeaddree.BuildingNo.ToString();
                        txtFhFloorNo.Text = myfatherhomeaddree.FloorNo.ToString();
                        txtFhFlatNo.Text = myfatherhomeaddree.FlatNo.ToString();
                        txtFhStreetName.Text = myfatherhomeaddree.StreetName;
                        ddlFhStat_Id.SelectedValue = myfatherhomeaddree.Stat_Id.ToString();
                        ddlFhDistrict_Id.SelectedValue = myfatherhomeaddree.District_Id.ToString();
                        txtFhPostCode.Text = myfatherhomeaddree.PostCode.ToString();
                        txtFhPhone1.Text = myfatherhomeaddree.Phone1;
                        txtFhPhone2.Text = myfatherhomeaddree.Phone2;
                        txtFhPhone3.Text = myfatherhomeaddree.Phone3;
                    }
                }

                if (myfather.WorkAddress_Id != null && myfather.WorkAddress_Id != 0)
                {
                    SD_WorkAddress myfatherworkaddree = WorkAddress.Single((long)(myfather.WorkAddress_Id));

                    if (myfatherworkaddree.Id != null && myfatherworkaddree.Id != 0)
                    {
                        ddlFCareer.SelectedValue = myfatherworkaddree.Career.ToString();
                        txtFCompanyName.Text = myfatherworkaddree.CompanyName;
                        txtFBuildingNo.Text = myfatherworkaddree.BuildingNo.ToString();
                        txtFFloorNo.Text = myfatherworkaddree.FloorNo.ToString();
                        txtFFlatNo.Text = myfatherworkaddree.FlatNo.ToString();
                        txtFStreetName.Text = myfatherworkaddree.StreetName;
                        ddlFStat_Id.SelectedValue = myfatherworkaddree.Stat_Id.ToString();
                        ddlFDistrict_Id.SelectedValue = myfatherworkaddree.District_Id.ToString();
                        txtFPostCode.Text = myfatherworkaddree.PostCode.ToString();
                        txtFPhone1.Text = myfatherworkaddree.Phone1;
                        txtFPhone2.Text = myfatherworkaddree.Phone2;
                        txtFPhone3.Text = myfatherworkaddree.Phone3;
                        txtFInternalPhoneExtension.Text = myfatherworkaddree.InternalPhoneExtension;
                        txtFFAX.Text = myfatherworkaddree.FAX;
                        txtFMobile1.Text = myfatherworkaddree.Mobile1;
                        txtFMobile2.Text = myfatherworkaddree.Mobile2;
                        txtFEmail1.Text = myfatherworkaddree.Email1;
                        txtFEmail2.Text = myfatherworkaddree.Email2;

                    }
                }

            }

            // fill mother fileds if father inserted ****************************************************************
            SD_Mother mymother = Mother.SingleByStudentId(mystudentid);

            SD_Mother myupdatemother = new SD_Mother();

            if (mymother != null)
            {

                myupdatemother.Id = mymother.Id;
                //  lblstudentid.Text = myupdatemother.Student_Id.ToString();
                txtMAr_FirstName.Text = mymother.Ar_FirstName;
                txtMAr_SecondName.Text = mymother.Ar_SecondName;
                txtMAr_ThirdNmae.Text = mymother.Ar_ThirdName;
                txtMAr_FourthName.Text = mymother.Ar_FourthName;
                txtMEn_FirstName.Text = mymother.En_FirstName;
                txtMEn_SecondName.Text = mymother.En_SecondName;
                txtMEn_ThirdName.Text = mymother.En_ThirdName;
                txtMEn_FourthName.Text = mymother.En_FourthName;
                txtMLoginName.Text = mymother.UserName;
                txtMLoginPassword.Text = mymother.Password;

                if (mymother.Gregorian_BirthDate != null)
                {
                    txtMGregorian_BirthDate.Text = mymother.Gregorian_BirthDate.ToString();
                }

                if (mymother.Hijri_BirthDate != null)
                {
                    txtMHijri_BirthDate.Text = mymother.Hijri_BirthDate.ToString();
                }

                txtMIdentification_Number.Text = mymother.Identification_Number;
                txtMPasport_Number.Text = mymother.Pasport_Number;

                if (mymother.Qualification_Id != null)
                {
                    ddlMQualification_Id.SelectedValue = mymother.Qualification_Id.ToString();

                }

                txtMSpecialization.Text = mymother.Specialization;
                txtMPointOfGraduation.Text = mymother.PointOfGraduation;

                if (mymother.Greggorian_GraduationDate != null)
                {
                    ddlMGreggorian_GraduationDate.SelectedItem.Text = mymother.Greggorian_GraduationDate.ToString();

                }

                if (mymother.Hijri_GraduationDate != null)
                {
                    ddlMHijri_GraduationDate.SelectedItem.Text = mymother.Hijri_GraduationDate.ToString();

                }

                if (mymother.Nationality_Id != null)
                {
                    lbMCountry_Id.SelectedValue = mymother.Nationality_Id.ToString();
                }


                if (mymother.HomeAddress_Id != null && mymother.HomeAddress_Id != 0)
                {
                    SD_HomeAddress mymotherhomeaddree = HomeAddress.Single((long)(mymother.HomeAddress_Id));
                    if (mymotherhomeaddree.Id != null && mymotherhomeaddree.Id != 0)
                    {
                        txtMhBuildingNo.Text = mymotherhomeaddree.BuildingNo.ToString();
                        txtMhFloorNo.Text = mymotherhomeaddree.FloorNo.ToString();
                        txtMhFlatNo.Text = mymotherhomeaddree.FlatNo.ToString();
                        txtMhStreetName.Text = mymotherhomeaddree.StreetName;
                        ddlMhStat_Id.SelectedValue = mymotherhomeaddree.Stat_Id.ToString();
                        ddlMhDistrict_Id.SelectedValue = mymotherhomeaddree.District_Id.ToString();
                        txtMhPostCode.Text = mymotherhomeaddree.PostCode.ToString();
                        txtMhPhone1.Text = mymotherhomeaddree.Phone1;
                        txtMhPhone2.Text = mymotherhomeaddree.Phone2;
                        txtMhPhone3.Text = mymotherhomeaddree.Phone3;
                    }
                }


                if (mymother.WorkAddress_Id != null && mymother.WorkAddress_Id != 0)
                {

                    SD_WorkAddress mymotherrworkaddree = WorkAddress.Single((long)(mymother.WorkAddress_Id));

                    if (mymotherrworkaddree.Id != null && mymotherrworkaddree.Id != 0)
                    {
                        ddlMCareer.SelectedValue = mymotherrworkaddree.Career.ToString();
                        txtMCompanyName.Text = mymotherrworkaddree.CompanyName;
                        txtMBuildingNo.Text = mymotherrworkaddree.BuildingNo.ToString();
                        txtMFloorNo.Text = mymotherrworkaddree.FloorNo.ToString();
                        txtMFlatNo.Text = mymotherrworkaddree.FlatNo.ToString();
                        txtMStreetName.Text = mymotherrworkaddree.StreetName;
                        ddlMStat_Id.SelectedValue = mymotherrworkaddree.Stat_Id.ToString();
                        ddlMDistrict_Id.SelectedValue = mymotherrworkaddree.District_Id.ToString();
                        txtMPostCode.Text = mymotherrworkaddree.PostCode.ToString();
                        txtMPhone1.Text = mymotherrworkaddree.Phone1;
                        txtMPhone2.Text = mymotherrworkaddree.Phone2;
                        txtMPhone3.Text = mymotherrworkaddree.Phone3;
                        txtMInternalPhoneExtension.Text = mymotherrworkaddree.InternalPhoneExtension;
                        txtMFAX.Text = mymotherrworkaddree.FAX;
                        txtMMobile1.Text = mymotherrworkaddree.Mobile1;
                        txtMMobile2.Text = mymotherrworkaddree.Mobile2;
                        txtMEmail1.Text = mymotherrworkaddree.Email1;
                        txtMEmail2.Text = mymotherrworkaddree.Email2;
                    }
                }
            }
            // fill custody fileds if father inserted ****************************************************************

            SD_Custody mycustody = Custody.SingleByStudentId(mystudentid);

            SD_Custody myupdatecustody = new SD_Custody();

            if (mycustody != null)
            {
                myupdatecustody.Id = mycustody.Id;

                lbParentsSocialStatus_Id.SelectedValue = mycustody.ParentsSocialStatus_Id.ToString();
                lbCustodyType_Id.SelectedValue = mycustody.CustodyType_Id.ToString();
                lbStudentLivingPlace_Id.SelectedValue = mycustody.StudentLivingPlace_Id.ToString();
                txtCRelationToStudent.Text = mycustody.RelationToStudent;


                //  lblstudentid.Text = myupdatecustody.Student_Id.ToString();
                txtCAr_FirstName.Text = mycustody.Ar_FirstName;
                txtCAr_SecondName.Text = mycustody.Ar_SecondName;
                txtCAr_ThirdNmae.Text = mycustody.Ar_ThirdName;
                txtCAr_FourthName.Text = mycustody.Ar_FourthName;
                txtCEn_FirstName.Text = mycustody.En_FirstName;
                txtCEn_SecondName.Text = mycustody.En_SecondName;
                txtCEn_ThirdName.Text = mycustody.En_ThirdName;
                txtCEn_FourthName.Text = mycustody.En_FourthName;
                txtCEnLoginName.Text = mycustody.UserName;
                txtCEnLoginPassword.Text = mycustody.Password;


                if (mycustody.Gregorian_BirthDate != null)
                {
                    txtCGregorian_BirthDate.Text = mycustody.Gregorian_BirthDate.ToString();

                }

                if (mycustody.Hijri_BirthDate != null)
                {
                    txtCHijri_BirthDate.Text = mycustody.Hijri_BirthDate.ToString();


                }


                txtCIdentification_Number.Text = mycustody.Identification_Number;
                txtCPasport_Number.Text = mycustody.Pasport_Number;

                if (mycustody.Qualification_Id != null)
                {
                    ddlCQualification_Id.SelectedValue = mycustody.Qualification_Id.ToString();


                }
                txtCSpecialization.Text = mycustody.Specialization;
                txtCPointOfGraduation.Text = mycustody.PointOfGraduation;

                if (mycustody.Greggorian_GraduationDate != null)
                {
                    ddlCGreggorian_GraduationDate.SelectedItem.Text =mycustody.Greggorian_GraduationDate.ToString();


                }

                if (mycustody.Hijri_GraduationDate != null)
                {
                   ddlCHijri_GraduationDate.SelectedItem .Text = mycustody.Hijri_GraduationDate.ToString();

                }

                if (mycustody.Nationality_Id != null)
                {
                    lbCCountry_Id.SelectedValue = mycustody.Nationality_Id.ToString();
                }

                if (mycustody.HomeAddress_Id != null && mycustody.HomeAddress_Id != 0)
                {

                    SD_HomeAddress mycustodyhomeaddree = HomeAddress.Single((long)(mycustody.HomeAddress_Id));
                    if (mycustodyhomeaddree.Id != null && mycustodyhomeaddree.Id != 0)
                    {

                        txtChBuildingNo.Text = mycustodyhomeaddree.BuildingNo.ToString();
                        txtChFloorNo.Text = mycustodyhomeaddree.FloorNo.ToString();
                        txtChFlatNo.Text = mycustodyhomeaddree.FlatNo.ToString();
                        txtChStreetName.Text = mycustodyhomeaddree.StreetName;
                        ddlChStat_Id.SelectedValue = mycustodyhomeaddree.Stat_Id.ToString();
                        ddlChDistrict_Id.SelectedValue = mycustodyhomeaddree.District_Id.ToString();
                        txtChPostCode.Text = mycustodyhomeaddree.PostCode.ToString();
                        txtChPhone1.Text = mycustodyhomeaddree.Phone1;
                        txtChPhone2.Text = mycustodyhomeaddree.Phone2;
                        txtChPhone3.Text = mycustodyhomeaddree.Phone3;
                    }
                }

                if (mycustody.WorkAddress_Id != null && mycustody.WorkAddress_Id != 0)
                {
                    SD_WorkAddress mycustodyrworkaddree = WorkAddress.Single((long)(mycustody.WorkAddress_Id));

                    if (mycustodyrworkaddree.Id != null && mycustodyrworkaddree.Id != 0)
                    {
                        ddlCCareer.SelectedValue = mycustodyrworkaddree.Career.ToString();
                        txtCCompanyName.Text = mycustodyrworkaddree.CompanyName;
                        txtCBuildingNo.Text = mycustodyrworkaddree.BuildingNo.ToString();
                        txtCFloorNo.Text = mycustodyrworkaddree.FloorNo.ToString();
                        txtCFlatNo.Text = mycustodyrworkaddree.FlatNo.ToString();
                        txtCStreetName.Text = mycustodyrworkaddree.StreetName;
                        ddlCStat_Id.SelectedValue = mycustodyrworkaddree.Stat_Id.ToString();
                        ddlCDistrict_Id.SelectedValue = mycustodyrworkaddree.District_Id.ToString();
                        txtCPostCode.Text = mycustodyrworkaddree.PostCode.ToString();
                        txtCPhone1.Text = mycustodyrworkaddree.Phone1;
                        txtCPhone2.Text = mycustodyrworkaddree.Phone2;
                        txtCPhone3.Text = mycustodyrworkaddree.Phone3;
                        txtCInternalPhoneExtension.Text = mycustodyrworkaddree.InternalPhoneExtension;
                        txtCFAX.Text = mycustodyrworkaddree.FAX;
                        txtCMobile1.Text = mycustodyrworkaddree.Mobile1;
                        txtCMobile2.Text = mycustodyrworkaddree.Mobile2;
                        txtCEmail1.Text = mycustodyrworkaddree.Email1;
                        txtCEmail2.Text = mycustodyrworkaddree.Email2;
                    }
                }
            }


            //fill xschooles 

            IEnumerable<SD_XSchool> myxschool = XSchool.SingleByStudentId(mystudentid);
            if (myxschool.Count() != 0)
            {
                Session["dt"] = null;
                dt = new DataTable("Xschools");
                DataColumn col = dt.Columns.Add("id", typeof(Int32));
                col.AllowDBNull = false;
                col.Unique = true;
                dt.Columns.Add("Student_Id", typeof(long));
                col.AllowDBNull = false;
                dt.Columns.Add("اسم المدرسة", typeof(String));
                dt.Columns.Add("الفترة من", typeof(DateTime));
                dt.Columns.Add("الفترة الى", typeof(DateTime));
                dt.Columns.Add("العنوان", typeof(String));
                dt.Columns.Add("الشهادة", typeof(String));
                dt.Columns.Add("المرحلة", typeof(String));
                dt.Columns.Add("الصف", typeof(String));
                dt.Columns.Add("اللغة الاولى", typeof(String));
                dt.Columns.Add("اللغة الثانية", typeof(String));

                DataRow row;
                int x;
                for (x = 0; x <= myxschool.Count() - 1; x++)
                {
                    row = dt.NewRow();

                    if (Session["dt"] != null)
                    {
                        int intid = 0;
                        for (i = 0; i <= ((DataTable)Session["dt"]).Rows.Count - 1; i++)
                        {

                            intid = intid + 1;

                        }

                        row["id"] = intid + 1;
                    }

                    else
                    {
                        row["id"] = 1;


                    }

                    row["Student_Id"] = long.Parse(lblstudentid.Text);
                    row["اسم المدرسة"] = myxschool.ElementAt(x).SchoolName;
                    row["الفترة من"] = DateTime.Parse(myxschool.ElementAt(x).SatrtDate.ToString()).ToShortDateString();
                    row["الفترة الى"] = DateTime.Parse(myxschool.ElementAt(x).EndDate.ToString()).ToShortDateString();
                    row["العنوان"] = myxschool.ElementAt(x).Address;
                    row["الشهادة"] = myxschool.ElementAt(x).Certification;
                    row["المرحلة"] = myxschool.ElementAt(x).Grade;
                    row["الصف"] = myxschool.ElementAt(x).Class; ;
                    row["اللغة الاولى"] = myxschool.ElementAt(x).FirstLanguage; ;
                    row["اللغة الثانية"] = myxschool.ElementAt(x).SecondLanguage; ;
                    dt.Rows.Add(row);


                    if (Session["dt"] != null)
                    {
                        dt.Merge((DataTable)Session["dt"]);
                    }
                    Session["dt"] = dt;


                }

                griddataxschools.DataSource = dt;
                griddataxschools.DataBind();
            }
            //fill realtives *******************************************************************
            var db = new SchoolEntities();
            IEnumerable<TE_Student_Relatives> mystudentrelative = StudentRelatives.SingleByStudentId(mystudentid);

            if (mystudentrelative.Count() != 0)
            {
                Session["dtmystudent"] = null;
                dtmystudent = new DataTable("relativestudent");
                DataColumn colrelative = dtmystudent.Columns.Add("id", typeof(Int32));
                colrelative.AllowDBNull = false;
                colrelative.Unique = true;

                dtmystudent.Columns.Add("Student_Id", typeof(long));
                colrelative.AllowDBNull = false;

                dtmystudent.Columns.Add("RelativeStudent_Id", typeof(long));
                colrelative.AllowDBNull = false;

                dtmystudent.Columns.Add("اسم الطالب", typeof(String));
                dtmystudent.Columns.Add("نوع الصلة", typeof(String));
                DataRow rowrelative;
                int y;
                for (y = 0; y <= mystudentrelative.Count() - 1; y++)
                {
                    rowrelative = dtmystudent.NewRow();

                    if (Session["dtmystudent"] != null)
                    {
                        int z = 0;
                        for (i = 0; i <= ((DataTable)Session["dtmystudent"]).Rows.Count - 1; i++)
                        {

                            z = z + 1;

                        }

                        rowrelative["id"] = z + 1;
                    }

                    else
                    {
                        rowrelative["id"] = 1;


                    }

                    rowrelative["Student_Id"] = long.Parse(lblstudentid.Text);
                    rowrelative["RelativeStudent_Id"] = mystudentrelative.ElementAt(y).Relative_student_Id;
                    long? koko = mystudentrelative.ElementAt(y).Relative_student_Id;
                    string Fullname = db.TE_Student.Where(st => st.Id == koko).Select(st => new
                    {
                        FullName = st.Ar_FirstName + " " + st.Ar_SecondName + " " + st.Ar_ThirdNmae + " " + st.Ar_FourthName
                    }).First().FullName;
                    rowrelative["اسم الطالب"] = Fullname;
                    rowrelative["نوع الصلة"] = mystudentrelative.ElementAt(y).Details;
                    dtmystudent.Rows.Add(rowrelative);

                    if (Session["dtmystudent"] != null)
                    {
                        dtmystudent.Merge((DataTable)Session["dtmystudent"]);
                    }
                    Session["dtmystudent"] = dtmystudent;

                }


                griddatarelative.DataSource = dtmystudent;
                griddatarelative.DataBind();

            }
            //fill StudentQualificatioSkill
            QualificationSkillRepeater.DataSource = LookUps.QualificationSkill();
            QualificationSkillRepeater.DataBind();

            IEnumerable<SD_StudentQualificatioSkill> myqsstudent = StudentQualificatioSkill.SingleByStudentId(mystudentid);

            int j;
            for (j = 0; j <= QualificationSkillRepeater.Items.Count - 1; j++)
            {


                Label lblid = default(Label);
                lblid = QualificationSkillRepeater.Items[j].FindControl("lblID") as Label;
                String id = "";
                id = lblid.Text;

                TextBox txtfdet = default(TextBox);
                txtfdet = QualificationSkillRepeater.Items[j].FindControl("txtDetail") as TextBox;
                txtfdet.Text = myqsstudent.ElementAt(j).Detail;

                Boolean isexist;
                CheckBox mycbIsExist = default(CheckBox);
                mycbIsExist = QualificationSkillRepeater.Items[j].FindControl("cbIsExist") as CheckBox;
                mycbIsExist.Checked = myqsstudent.ElementAt(j).IsExist;
            }


            //fill emergancy 

            SD_Emergency myemergency = Emergency.SingleByStudentId(mystudentid);

            txtName1.Text = myemergency.Name1;
            txtName2.Text = myemergency.Name2;
            txtName3.Text = myemergency.Name3;
            txtName4.Text = myemergency.Name4;
            txtName5.Text = myemergency.Name5;

            txtPhone1.Text = myemergency.Phone1;
            txtPhone2.Text = myemergency.Phone2;
            txtPhone3.Text = myemergency.Phone3;
            txtPhone4.Text = myemergency.Phone4;
            txtPhone5.Text = myemergency.Phone5;

            txtMobilePhone1.Text = myemergency.MobilePhone1;
            txtMobilePhone2.Text = myemergency.MobilePhone2;
            txtMobilePhone3.Text = myemergency.MobilePhone3;
            txtMobilePhone4.Text = myemergency.MobilePhone4;
            txtMobilePhone5.Text = myemergency.MobilePhone5;
        }

        protected void AddNewStudent()
        {

            TE_Student mystudent = new TE_Student();
            //  mystudent.Id =  long.Parse(lblstudentid.Text);
            long mystudentid = long.Parse(lblstudentid.Text);

            mystudent.Id = mystudentid;

            long myFileNumber = long.Parse(txtFileNumber.Text);
            mystudent.FileNumber = myFileNumber;

            long myRegistrationNumber = txtRegistrationNumber.Text != "" ? long.Parse(txtRegistrationNumber.Text) : 0;
            mystudent.RegistrationNumber = myRegistrationNumber;

            mystudent.Campus_Id = long.Parse(ddlCampus.SelectedValue);
            mystudent.Certificate_Id = long.Parse(ddlCertificate.SelectedValue);
            mystudent.Stage_Id = long.Parse(ddlStadge.SelectedValue);
            mystudent.Grade_ID = long.Parse(ddltopgrad.SelectedValue);

            mystudent.Ar_FirstName = txtAr_FirstName.Text;
            mystudent.Ar_SecondName = txtAr_SecondName.Text;
            mystudent.Ar_ThirdNmae = txtAr_ThirdNmae.Text;
            mystudent.Ar_FourthName = txtAr_FourthName.Text;
            mystudent.En_FirstName = txtEn_FirstName.Text;
            mystudent.En_SecondName = txtEn_SecondName.Text;
            mystudent.En_ThirdName = txtEn_ThirdName.Text;
            mystudent.En_FourthName = txtEn_FourthName.Text;
            mystudent.UserName = txtStLoginName.Text;
            mystudent.Password = txtStLoginPassword.Text;

            if (txtGregorian_BirthDate.Text.Length != 0)
            {
                mystudent.Gregorian_BirthDate = DateTime.Parse(txtGregorian_BirthDate.Text);
            }

            if (txtHijri_BirthDate.Text.Length != 0)
            {
                mystudent.Hijri_BirthDate = DateTime.Parse(txtHijri_BirthDate.Text);
            }



            mystudent.Gender_Id = int.Parse(ddlGender_Id.SelectedValue);
            mystudent.Religion_Id = int.Parse(ddlReligion_Id.SelectedValue);

            mystudent.Country_Id = int.Parse(lbCountry_Id.SelectedValue);

            mystudent.ID_Number = txtID_Number.Text;

            if (txtID_Gregorian_Issue_Date.Text.Length != 0)
            {
                mystudent.ID_Gregorian_Issue_Date = DateTime.Parse(txtID_Gregorian_Issue_Date.Text);

            }

            if (txtID_Hijri_Issue_Date1.Text.Length != 0)
            {
                mystudent.ID_Hijri_Issue_Date1 = DateTime.Parse(txtID_Hijri_Issue_Date1.Text);


            }

            mystudent.Passport_Number = txtPassport_Number.Text;


            if (txtPassport_Gregorian_Issue_Date.Text.Length != 0)
            {
                mystudent.Passport_Gregorian_Issue_Date = DateTime.Parse(txtPassport_Gregorian_Issue_Date.Text);

            }

            if (txtPassport_Hijri_Issue_Date1.Text.Length != 0)
            {
                mystudent.Passport_Hijri_Issue_Date1 = DateTime.Parse(txtPassport_Hijri_Issue_Date1.Text);

            }

            mystudent.Arrangement_bt_Brothers = int.Parse(ddlArrangement_bt_Brothers.SelectedValue);
            mystudent.Used_Home_Languages = lbUsed_Home_Languages.SelectedItem.Text;

            Student.Update(mystudent);


        }

        # endregion

        protected void btnaddtogrid_Click(object sender, EventArgs e)
        {

            if (txtSchoolName.Text == "" || txtSatrtDate.Text == "" || txtEndDate.Text == "" || ddlCertification.SelectedIndex == 0 || ddlGrade.SelectedIndex == 0 || ddlClass.SelectedIndex == 0)
            {
                lblschoolmessage.Text = "من فضلك ادخل البيانات بشكل صحيح ";
                return;
            }

            else
            {
                lblschoolmessage.Text = "";
                dt = new DataTable("Xschools");

                DataColumn col = dt.Columns.Add("id", typeof(Int32));
                col.AllowDBNull = false;
                col.Unique = true;


                dt.Columns.Add("Student_Id", typeof(long));
                col.AllowDBNull = false;

                dt.Columns.Add("اسم المدرسة", typeof(String));
                dt.Columns.Add("الفترة من", typeof(DateTime));
                dt.Columns.Add("الفترة الى", typeof(DateTime));
                dt.Columns.Add("العنوان", typeof(String));
                dt.Columns.Add("الشهادة", typeof(String));
                dt.Columns.Add("المرحلة", typeof(String));
                dt.Columns.Add("الصف", typeof(String));
                dt.Columns.Add("اللغة الاولى", typeof(String));
                dt.Columns.Add("اللغة الثانية", typeof(String));

                DataRow row;

                row = dt.NewRow();

                if (Session["dt"] != null)
                {
                    int x = 0;
                    for (i = 0; i <= ((DataTable)Session["dt"]).Rows.Count - 1; i++)
                    {

                        x = x + 1;

                    }

                    row["id"] = x + 1;
                }

                else
                {
                    row["id"] = 1;


                }

                row["Student_Id"] = long.Parse(lblstudentid.Text);
                row["اسم المدرسة"] = txtSchoolName.Text;

                var datefromparsed = DateTime.Parse(txtSatrtDate.Text).ToShortDateString();
                DateTime SDate = Convert.ToDateTime(datefromparsed);
                row["الفترة من"] = txtSatrtDate.Text != "" ? datefromparsed : Convert.ToDateTime(DBNull.Value).ToShortDateString();

                var dateparsed = DateTime.Parse(txtEndDate.Text).ToShortDateString();
                DateTime Edate = DateTime.Parse(dateparsed);

                row["الفترة الى"] = txtEndDate.Text != "" ? dateparsed : Convert.ToDateTime(DBNull.Value).ToShortDateString();

                row["العنوان"] = txtAddress.Text;
                row["الشهادة"] = ddlCertification.SelectedItem.Text;
                row["المرحلة"] = ddlGrade.SelectedItem.Text;
                row["الصف"] = ddlClass.SelectedItem.Text;
                row["اللغة الاولى"] = ddlFirstLanguage.SelectedItem.Text;
                row["اللغة الثانية"] = ddlSecondLanguage.SelectedItem.Text;
                dt.Rows.Add(row);


                if (Session["dt"] != null)
                {
                    dt.Merge((DataTable)Session["dt"]);
                }
                Session["dt"] = dt;



                griddataxschools.DataSource = dt;
                griddataxschools.DataBind();

                clearmyschool();
            }
        }

        protected void griddataxschools_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "OnDelete")
            {
                GridViewRow selectedRow = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
                DataKey shoolid = griddataxschools.DataKeys[selectedRow.RowIndex];

                if (Session["dt"] != null)
                {
                    ((DataTable)Session["dt"]).Rows[selectedRow.RowIndex].Delete();

                }
            }
            griddataxschools.DataSource = Session["dt"];
            griddataxschools.DataBind();

        }

        protected void griddataxschools_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            e.Row.Cells[1].Visible = false;
            e.Row.Cells[2].Visible = false;

        }


        protected void btnSaveAll_Click(object sender, EventArgs e)
        {
            long mystudentid = long.Parse(lblstudentid.Text);
            Session["FIdentification_Number"] = txtFIdentification_Number.Text.Trim();

            AddNewStudent();
            handelfather(mystudentid);
            handelmother(mystudentid);
            handelcustody(mystudentid);
            handelxschool(mystudentid);
            handelStudentQualificatioSkill(mystudentid);
            handelemergancy(mystudentid);
            handelrelative(mystudentid);
            Session["FIdentification_Number"] = "";
            
            Response.Redirect("messages.aspx");
        }
        protected void btnCheckStudentUserName_Click(object sender, EventArgs e)
        {
            long mystudentid = long.Parse(lblstudentid.Text);
            lblCheckStudentUserNameMessage.Visible = true;
            if (!Student.CheckUserNameFound(txtStLoginName.Text, mystudentid))
            {
                lblCheckStudentUserNameMessage.Text = "اسم المستخدم غير موجود من قبل";
            }
        }

        protected void btnCheckFatherUserName_Click(object sender, EventArgs e)
        {
            long mystudentid = long.Parse(lblstudentid.Text);
            SD_Father myfather = Father.SingleByStudentId(mystudentid);
            long myfatherid = 0;
            if (myfather != null)
            {
                 myfatherid = myfather.Id;
                
            }

            lblCheckFatherUserNameMessage.Visible = true;
            if (!Father.CheckUserNameFound(txtFLoginName.Text,myfatherid))
            {
                lblCheckFatherUserNameMessage.Text= "اسم المستخدم غير موجود من قبل";
            }
        }

        //handel events on father 
        protected void handelfather(long mystudentid)
        {
            SD_Father myfather = Father.SingleByStudentId(mystudentid);
            if (myfather == null)
            {
                AddFatherToStudent();
                lblmessages.Text = "";
            }
            else
            {
                long myfatherid = myfather.Id;
                updatefatherwithstudent(myfatherid);
            }

        }

        /// Add New Father data 
        protected void AddFatherToStudent()
        {
            // add home addreess to father 

            var newhomeaddress = new SD_HomeAddress()
            {
                BuildingNo = txtFhBuildingNo.Text != "" ? int.Parse(txtFhBuildingNo.Text) : 0,
                FloorNo = txtFhFloorNo.Text != "" ? int.Parse(txtFhFloorNo.Text) : 0,
                FlatNo = txtFhFlatNo.Text != "" ? int.Parse(txtFhFlatNo.Text) : 0,
                StreetName = txtFhStreetName.Text,
                PostCode = txtFhPostCode.Text != "" ? int.Parse(txtFhPostCode.Text) : 0,
                Phone1 = txtFhPhone1.Text,
                Phone2 = txtFhPhone2.Text,
                Phone3 = txtFhPhone3.Text,


            };

            if (ddlFhStat_Id.SelectedValue != "0")
            {
                newhomeaddress.Stat_Id = long.Parse(ddlFhStat_Id.SelectedValue);

                if (ddlFhDistrict_Id.SelectedValue != "0")
                    newhomeaddress.District_Id = long.Parse(ddlFhDistrict_Id.SelectedValue);
            }
            myhomeaddressid = HomeAddress.Add(newhomeaddress);


            // add work addreess to father 
            var newworkaddress = new SD_WorkAddress()

            {
                Career = ddlFCareer.SelectedValue,
                CompanyName = txtFCompanyName.Text,
                BuildingNo = txtFBuildingNo.Text != "" ? int.Parse(txtFBuildingNo.Text) : 0,
                FloorNo = txtFFloorNo.Text != "" ? int.Parse(txtFFloorNo.Text) : 0,
                FlatNo = txtFFlatNo.Text != "" ? int.Parse(txtFFlatNo.Text) : 0,
                StreetName = txtFStreetName.Text,
                PostCode = txtFPostCode.Text != "" ? int.Parse(txtFPostCode.Text) : 0,
                Phone1 = txtFPhone1.Text,
                Phone2 = txtFPhone2.Text,
                Phone3 = txtFPhone3.Text,
                InternalPhoneExtension = txtFInternalPhoneExtension.Text,
                FAX = txtFFAX.Text,
                Mobile1 = txtFMobile1.Text,
                Mobile2 = txtFMobile2.Text,
                Email1 = txtFEmail1.Text,
                Email2 = txtFEmail2.Text,
            };

            if (ddlFStat_Id.SelectedValue != "0")
            {
                newworkaddress.Stat_Id = long.Parse(ddlFStat_Id.SelectedValue);

                if (ddlFDistrict_Id.SelectedValue != "0")
                    newworkaddress.District_Id = long.Parse(ddlFDistrict_Id.SelectedValue);
            }
            myworkaddressid = WorkAddress.Add(newworkaddress);



            if (myhomeaddressid == 0)
            {
                myhomeaddressid = null;
            }

            if (myworkaddressid == 0)
            {
                myworkaddressid = null;
            }


            var newfather = new SD_Father()

               {
                   Student_Id = long.Parse(lblstudentid.Text),

                   Ar_FirstName = txtFAr_FirstName.Text,
                   Ar_SecondName = txtFAr_SecondName.Text,
                   Ar_ThirdName = txtFAr_ThirdNmae.Text,
                   Ar_FourthName = txtFAr_FourthName.Text,
                   En_FirstName = txtFEn_FirstName.Text,
                   En_SecondName = txtFEn_SecondName.Text,
                   En_ThirdName = txtFEn_ThirdName.Text,
                   En_FourthName = txtFEn_FourthName.Text, 
                   UserName = txtFLoginName.Text,
                   Password = txtFloginPassword.Text,

                   Identification_Number = txtFIdentification_Number.Text,
                   Pasport_Number = txtFPasport_Number.Text,
                   Qualification_Id = long.Parse(ddlFQualification_Id.SelectedValue),
                   Specialization = txtFSpecialization.Text,
                   PointOfGraduation = txtFPointOfGraduation.Text,

                   HomeAddress_Id = myhomeaddressid,
                   WorkAddress_Id = myworkaddressid,
                   Nationality_Id = long.Parse(lbFCountry_Id.SelectedValue),
                   Gregorian_BirthDate = txtFGregorian_BirthDate.Text,
                   
                   Greggorian_GraduationDate = ddlFGreggorian_GraduationDate.SelectedValue.ToString() 
               };

            
            Father.Add(newfather);

        }

        // Update exist father 
        protected void updatefatherwithstudent(long myfatherid)
        {

            long myupdatehomeaddressid;

            if (HomeAddress.SingleByfatherId(myfatherid) == null)
            {
                var newupdatehomeaddress = new SD_HomeAddress()

                {
                    BuildingNo = txtFhBuildingNo.Text != "" ? int.Parse(txtFhBuildingNo.Text) : 0,
                    FloorNo = txtFhFloorNo.Text != "" ? int.Parse(txtFhFloorNo.Text) : 0,
                    FlatNo = txtFhFlatNo.Text != "" ? int.Parse(txtFhFlatNo.Text) : 0,
                    StreetName = txtFhStreetName.Text,
                    PostCode = txtFhPostCode.Text != "" ? int.Parse(txtFhPostCode.Text) : 0,
                    Phone1 = txtFhPhone1.Text,
                    Phone2 = txtFhPhone2.Text,
                    Phone3 = txtFhPhone3.Text,

                };

                if (ddlFhStat_Id.SelectedValue != "0")
                {
                    newupdatehomeaddress.Stat_Id = long.Parse(ddlFhStat_Id.SelectedValue);

                    if (ddlFhDistrict_Id.SelectedValue != "0")
                        newupdatehomeaddress.District_Id = long.Parse(ddlFhDistrict_Id.SelectedValue);
                }
                myupdatehomeaddressid = HomeAddress.Add(newupdatehomeaddress);


            }
            else
            {
                myupdatehomeaddressid = HomeAddress.SingleByfatherId(myfatherid).Id;
                var newupdatehomeaddress = new SD_HomeAddress
                 {

                     Id = myupdatehomeaddressid,
                     BuildingNo = txtFhBuildingNo.Text != "" ? int.Parse(txtFhBuildingNo.Text) : 0,
                     FloorNo = txtFhFloorNo.Text != "" ? int.Parse(txtFhFloorNo.Text) : 0,
                     FlatNo = txtFhFlatNo.Text != "" ? int.Parse(txtFhFlatNo.Text) : 0,
                     StreetName = txtFhStreetName.Text,
                     PostCode = txtFhPostCode.Text != "" ? int.Parse(txtFhPostCode.Text) : 0,
                     Phone1 = txtFhPhone1.Text,
                     Phone2 = txtFhPhone2.Text,
                     Phone3 = txtFhPhone3.Text,

                 };

                if (ddlFhStat_Id.SelectedValue != "0")
                {
                    newupdatehomeaddress.Stat_Id = long.Parse(ddlFhStat_Id.SelectedValue);
                    if (ddlFhDistrict_Id.SelectedValue != "0")
                    {
                        try
                        {
                            newupdatehomeaddress.District_Id = long.Parse(ddlFhDistrict_Id.SelectedValue);
                        }
                        catch
                        {

                        }
                    }
                }
                HomeAddress.Update(newupdatehomeaddress);


            }


            // add work addreess to father 
            long myupdateworkaddressid;
            if (WorkAddress.SingleByFatherId(myfatherid) == null)
            {
                var newupdateworkaddress = new SD_WorkAddress()

                {
                    Career = ddlFCareer.SelectedValue,
                    CompanyName = txtFCompanyName.Text,
                    BuildingNo = txtFBuildingNo.Text != "" ? int.Parse(txtFBuildingNo.Text) : 0,
                    FloorNo = txtFFloorNo.Text != "" ? int.Parse(txtFFloorNo.Text) : 0,
                    FlatNo = txtFFlatNo.Text != "" ? int.Parse(txtFFlatNo.Text) : 0,
                    StreetName = txtFStreetName.Text,
                    PostCode = txtFPostCode.Text != "" ? int.Parse(txtFPostCode.Text) : 0,
                    Phone1 = txtFPhone1.Text,
                    Phone2 = txtFPhone2.Text,
                    Phone3 = txtFPhone3.Text,
                    InternalPhoneExtension = txtFInternalPhoneExtension.Text,
                    FAX = txtFFAX.Text,
                    Mobile1 = txtFMobile1.Text,
                    Mobile2 = txtFMobile2.Text,
                    Email1 = txtFEmail1.Text,
                    Email2 = txtFEmail2.Text,
                };

                if (ddlFStat_Id.SelectedValue != "0")
                {
                    newupdateworkaddress.Stat_Id = long.Parse(ddlFStat_Id.SelectedValue);
                    if (ddlFDistrict_Id.SelectedValue != "0")
                    {
                        try
                        {
                            newupdateworkaddress.District_Id = long.Parse(ddlFDistrict_Id.SelectedValue);
                        }
                        catch { }
                    }
                }
                myupdateworkaddressid = WorkAddress.Add(newupdateworkaddress);

            }


            else
            {
                myupdateworkaddressid = WorkAddress.SingleByFatherId(myfatherid).Id;

                var newupdateworkaddress = new SD_WorkAddress
                {
                    Id = myupdateworkaddressid,
                    Career = ddlFCareer.SelectedValue,
                    CompanyName = txtFCompanyName.Text,
                    BuildingNo = txtFBuildingNo.Text != "" ? int.Parse(txtFBuildingNo.Text) : 0,
                    FloorNo = txtFFloorNo.Text != "" ? int.Parse(txtFFloorNo.Text) : 0,
                    FlatNo = txtFFlatNo.Text != "" ? int.Parse(txtFFlatNo.Text) : 0,
                    StreetName = txtFStreetName.Text,
                    PostCode = txtFPostCode.Text != "" ? int.Parse(txtFPostCode.Text) : 0,
                    Phone1 = txtFPhone1.Text,
                    Phone2 = txtFPhone2.Text,
                    Phone3 = txtFPhone3.Text,
                    InternalPhoneExtension = txtFInternalPhoneExtension.Text,
                    FAX = txtFFAX.Text,
                    Mobile1 = txtFMobile1.Text,
                    Mobile2 = txtFMobile2.Text,
                    Email1 = txtFEmail1.Text,
                    Email2 = txtFEmail2.Text,

                };

                if (ddlFStat_Id.SelectedValue != "0")
                    newupdateworkaddress.Stat_Id = long.Parse(ddlFStat_Id.SelectedValue);
                if (ddlFDistrict_Id.SelectedValue != "0")
                    newupdateworkaddress.District_Id = long.Parse(ddlFDistrict_Id.SelectedValue);

                WorkAddress.Update(newupdateworkaddress);


            }

            SD_Father myupdatefather = new SD_Father();

            myupdatefather.Id = myfatherid;
            myupdatefather.Student_Id = long.Parse(lblstudentid.Text);
            myupdatefather.Ar_FirstName = txtFAr_FirstName.Text;
            myupdatefather.Ar_SecondName = txtFAr_SecondName.Text;
            myupdatefather.Ar_ThirdName = txtFAr_ThirdNmae.Text;
            myupdatefather.Ar_FourthName = txtFAr_FourthName.Text;
            myupdatefather.En_FirstName = txtFEn_FirstName.Text;
            myupdatefather.En_SecondName = txtFEn_SecondName.Text;
            myupdatefather.En_ThirdName = txtFEn_ThirdName.Text;
            myupdatefather.En_FourthName = txtFEn_FourthName.Text;
            myupdatefather.Password = txtFloginPassword.Text;
            myupdatefather.UserName = txtFLoginName.Text;

            if (txtFGregorian_BirthDate.Text.Length != 0)
            {
               myupdatefather.Gregorian_BirthDate = txtFGregorian_BirthDate.Text;
            }

            if (txtFHijri_BirthDate.Text.Length != 0)
            {
                myupdatefather.Hijri_BirthDate = txtFHijri_BirthDate.Text;
            }

            myupdatefather.Identification_Number = txtFIdentification_Number.Text;
            myupdatefather.Pasport_Number = txtFPasport_Number.Text;
            myupdatefather.Qualification_Id = long.Parse(ddlFQualification_Id.SelectedValue);
            myupdatefather.Specialization = txtFSpecialization.Text;
            myupdatefather.PointOfGraduation = txtFPointOfGraduation.Text;

            //if (txtFGreggorian_GraduationDate.Text.Length != 0)
            //{
            myupdatefather.Greggorian_GraduationDate = ddlFGreggorian_GraduationDate.SelectedItem.Text;
            //}

            //if (txtFHijri_GraduationDate.Text.Length != 0)
            //{
            myupdatefather.Hijri_GraduationDate = ddlFHijri_GraduationDate.SelectedItem.Text;
            //}
            myupdatefather.HomeAddress_Id = myupdatehomeaddressid;
            myupdatefather.WorkAddress_Id = myupdateworkaddressid;
            myupdatefather.Nationality_Id = long.Parse(lbFCountry_Id.SelectedValue);
            Father.Update(myupdatefather);

        }

        //handel events on mother 
        protected void handelmother(long mystudentid)
        {

            SD_Mother mymother = Mother.SingleByStudentId(mystudentid);
            if (mymother == null)
            {
                AddMotherToStudent();
                lblmessages.Text = "";
            }

            else
            {
                long mymotherid = mymother.Id;
                updatemotherwithstudent(mymotherid);
            }

        }

        // Add new mother to curant  student 
        protected void AddMotherToStudent()
        {

            if (chekboxthesameaddreeforfather.Checked == true)
            {
                var newhomeaddress = new SD_HomeAddress()

                {
                    BuildingNo = txtFhBuildingNo.Text != "" ? int.Parse(txtFhBuildingNo.Text) : 0,
                    FloorNo = txtFhFloorNo.Text != "" ? int.Parse(txtFhFloorNo.Text) : 0,
                    FlatNo = txtFhFlatNo.Text != "" ? int.Parse(txtFhFlatNo.Text) : 0,
                    StreetName = txtFhStreetName.Text,
                    PostCode = txtFhPostCode.Text != "" ? int.Parse(txtFhPostCode.Text) : 0,
                    Phone1 = txtFhPhone1.Text,
                    Phone2 = txtFhPhone2.Text,
                    Phone3 = txtFhPhone3.Text,

                };

                if (ddlFhStat_Id.SelectedValue != "0")
                {
                    newhomeaddress.Stat_Id = long.Parse(ddlFhStat_Id.SelectedValue);
                    if (ddlFhDistrict_Id.SelectedValue != "0")
                    {
                        try
                        {
                            newhomeaddress.District_Id = long.Parse(ddlFhDistrict_Id.SelectedValue);
                        }
                        catch { }
                    }
                }
                myhomeaddressid = HomeAddress.Add(newhomeaddress);

            }

            else
            {
                var newhomeaddress = new SD_HomeAddress()

               {
                   BuildingNo = txtMhBuildingNo.Text != "" ? int.Parse(txtMhBuildingNo.Text) : 0,
                   FloorNo = txtMhFloorNo.Text != "" ? int.Parse(txtMhFloorNo.Text) : 0,
                   FlatNo = txtMhFlatNo.Text != "" ? int.Parse(txtMhFlatNo.Text) : 0,
                   StreetName = txtMhStreetName.Text,
                   PostCode = txtMhPostCode.Text != "" ? int.Parse(txtMhPostCode.Text) : 0,
                   Phone1 = txtMhPhone1.Text,
                   Phone2 = txtMhPhone2.Text,
                   Phone3 = txtMhPhone3.Text,

               };

                if (ddlMhStat_Id.SelectedValue != "0")
                {
                    newhomeaddress.Stat_Id = long.Parse(ddlMhStat_Id.SelectedValue);
                    if (ddlMhDistrict_Id.SelectedValue != "0")
                    {
                        try
                        {
                            newhomeaddress.District_Id = long.Parse(ddlMhDistrict_Id.SelectedValue);
                        }
                        catch { }
                    }
                }
                myhomeaddressid = HomeAddress.Add(newhomeaddress);
            }

            var newworkaddress = new SD_WorkAddress()

           {
               Career = ddlMCareer.SelectedValue,
               CompanyName = txtMCompanyName.Text,
               BuildingNo = txtMBuildingNo.Text != "" ? int.Parse(txtMBuildingNo.Text) : 0,
               FloorNo = txtMFloorNo.Text != "" ? int.Parse(txtMFloorNo.Text) : 0,
               FlatNo = txtMFlatNo.Text != "" ? int.Parse(txtMFlatNo.Text) : 0,
               StreetName = txtMStreetName.Text,
               PostCode = txtMPostCode.Text != "" ? int.Parse(txtMPostCode.Text) : 0,
               Phone1 = txtMPhone1.Text,
               Phone2 = txtMPhone2.Text,
               Phone3 = txtMPhone3.Text,
               InternalPhoneExtension = txtMInternalPhoneExtension.Text,
               FAX = txtMFAX.Text,
               Mobile1 = txtMMobile1.Text,
               Mobile2 = txtMMobile2.Text,
               Email1 = txtMEmail1.Text,
               Email2 = txtMEmail2.Text,
           };

            if (ddlMStat_Id.SelectedValue != "0")
            {
                newworkaddress.Stat_Id = long.Parse(ddlMStat_Id.SelectedValue);

                if (ddlMDistrict_Id.SelectedValue != "0")
                {
                    try
                    {
                        newworkaddress.District_Id = long.Parse(ddlMDistrict_Id.SelectedValue);
                    }
                    catch
                    {
                    }
                }
            }
            myworkaddressid = WorkAddress.Add(newworkaddress);


            if (myhomeaddressid == 0)
            {
                myhomeaddressid = null;
            }

            if (myworkaddressid == 0)
            {
                myworkaddressid = null;
            }


            var newmother = new SD_Mother()

            {
                Student_Id = long.Parse(lblstudentid.Text),

                Ar_FirstName = txtMAr_FirstName.Text,
                Ar_SecondName = txtMAr_SecondName.Text,
                Ar_ThirdName = txtMAr_ThirdNmae.Text,
                Ar_FourthName = txtMAr_FourthName.Text,
                En_FirstName = txtMEn_FirstName.Text,
                En_SecondName = txtMEn_SecondName.Text,
                En_ThirdName = txtMEn_ThirdName.Text,
                En_FourthName = txtMEn_FourthName.Text,
                UserName = txtMLoginName.Text,
                Password = txtMLoginPassword.Text,

                Identification_Number = txtMIdentification_Number.Text,
                Pasport_Number = txtMPasport_Number.Text,
                Qualification_Id = long.Parse(ddlMQualification_Id.SelectedValue),
                Specialization = txtMSpecialization.Text,
                PointOfGraduation = txtMPointOfGraduation.Text,
                HomeAddress_Id = myhomeaddressid,
                WorkAddress_Id = myworkaddressid,
                Nationality_Id = long.Parse(lbMCountry_Id.SelectedValue),

            };

            if (txtMGregorian_BirthDate.Text.Length != 0)
                newmother.Gregorian_BirthDate = txtMGregorian_BirthDate.Text;

            if (txtMHijri_BirthDate.Text.Length != 0)
                newmother.Hijri_BirthDate =txtMHijri_BirthDate.Text;

            //if (txtMGreggorian_GraduationDate.Text.Length != 0)
            newmother.Greggorian_GraduationDate = ddlMGreggorian_GraduationDate.SelectedItem.Text;

            //if (txtMHijri_GraduationDate.Text.Length != 0)
                newmother.Hijri_GraduationDate = ddlMHijri_GraduationDate.SelectedItem.Text;


            Mother.Add(newmother);


        }

        // update exist mother to curant student 
        protected void updatemotherwithstudent(long mymotherid)
        {
            long myupdatehomeaddressid;

            if (chekboxthesameaddreeforfather.Checked == true)
            {
                if (HomeAddress.SingleByMotherId(mymotherid) == null)
                {

                    myupdatehomeaddressid = HomeAddress.Add(new SD_HomeAddress()


                    {

                        BuildingNo = txtFhBuildingNo.Text != "" ? int.Parse(txtFhBuildingNo.Text) : 0,
                        FloorNo = txtFhFloorNo.Text != "" ? int.Parse(txtFhFloorNo.Text) : 0,
                        FlatNo = txtFhFlatNo.Text != "" ? int.Parse(txtFhFlatNo.Text) : 0,
                        StreetName = txtFhStreetName.Text,
                        Stat_Id = long.Parse(ddlFhStat_Id.SelectedValue),
                        District_Id = long.Parse(ddlFhDistrict_Id.SelectedValue),
                        PostCode = txtFhPostCode.Text != "" ? int.Parse(txtFhPostCode.Text) : 0,
                        Phone1 = txtFhPhone1.Text,
                        Phone2 = txtFhPhone2.Text,
                        Phone3 = txtFhPhone3.Text,

                    }

    );
                }

                else
                {
                    myupdatehomeaddressid = HomeAddress.SingleByMotherId(mymotherid).Id;
                    HomeAddress.Update(new SD_HomeAddress
                    {

                        Id = myupdatehomeaddressid,

                        BuildingNo = txtFhBuildingNo.Text != "" ? int.Parse(txtFhBuildingNo.Text) : 0,
                        FloorNo = txtFhFloorNo.Text != "" ? int.Parse(txtFhFloorNo.Text) : 0,
                        FlatNo = txtFhFlatNo.Text != "" ? int.Parse(txtFhFlatNo.Text) : 0,
                        StreetName = txtFhStreetName.Text,
                        Stat_Id = long.Parse(ddlFhStat_Id.SelectedValue),
                        District_Id = long.Parse(ddlFhDistrict_Id.SelectedValue),
                        PostCode = txtFhPostCode.Text != "" ? int.Parse(txtFhPostCode.Text) : 0,
                        Phone1 = txtFhPhone1.Text,
                        Phone2 = txtFhPhone2.Text,
                        Phone3 = txtFhPhone3.Text,

                    }

                      );
                }

            }

            else
            {
                if (HomeAddress.SingleByMotherId(mymotherid) == null)
                {

                    myupdatehomeaddressid = HomeAddress.Add(new SD_HomeAddress()


                    {

                        BuildingNo = txtMhBuildingNo.Text != "" ? int.Parse(txtMhBuildingNo.Text) : 0,
                        FloorNo = txtMhFloorNo.Text != "" ? int.Parse(txtMhFloorNo.Text) : 0,
                        FlatNo = txtMhFlatNo.Text != "" ? int.Parse(txtMhFlatNo.Text) : 0,
                        StreetName = txtMhStreetName.Text,
                        Stat_Id = long.Parse(ddlMhStat_Id.SelectedValue),
                        District_Id = long.Parse(ddlMhDistrict_Id.SelectedValue),
                        PostCode = txtMhPostCode.Text != "" ? int.Parse(txtMhPostCode.Text) : 0,
                        Phone1 = txtMhPhone1.Text,
                        Phone2 = txtMhPhone2.Text,
                        Phone3 = txtMhPhone3.Text,

                    }

    );
                }

                else
                {
                    myupdatehomeaddressid = HomeAddress.SingleByMotherId(mymotherid).Id;
                    HomeAddress.Update(new SD_HomeAddress
                    {

                        Id = myupdatehomeaddressid,

                        BuildingNo = txtMhBuildingNo.Text != "" ? int.Parse(txtMhBuildingNo.Text) : 0,
                        FloorNo = txtMhFloorNo.Text != "" ? int.Parse(txtMhFloorNo.Text) : 0,
                        FlatNo = txtMhFlatNo.Text != "" ? int.Parse(txtMhFlatNo.Text) : 0,
                        StreetName = txtMhStreetName.Text,
                        Stat_Id = long.Parse(ddlMhStat_Id.SelectedValue),
                        District_Id = long.Parse(ddlMhDistrict_Id.SelectedValue),
                        PostCode = txtMhPostCode.Text != "" ? int.Parse(txtMhPostCode.Text) : 0,
                        Phone1 = txtMhPhone1.Text,
                        Phone2 = txtMhPhone2.Text,
                        Phone3 = txtMhPhone3.Text,

                    }

                      );
                }
            }
            // add work addreess to father 
            long myupdateworkaddressid;
            if (WorkAddress.SingleByFatherId(mymotherid) == null)
            {
                myupdateworkaddressid = WorkAddress.Add(new SD_WorkAddress()

                {

                    Career = ddlMCareer.SelectedValue,
                    CompanyName = txtMCompanyName.Text,
                    BuildingNo = txtMBuildingNo.Text != "" ? int.Parse(txtMBuildingNo.Text) : 0,
                    FloorNo = txtMFloorNo.Text != "" ? int.Parse(txtMFloorNo.Text) : 0,
                    FlatNo = txtMFlatNo.Text != "" ? int.Parse(txtMFlatNo.Text) : 0,
                    StreetName = txtMStreetName.Text,
                    Stat_Id = long.Parse(ddlMStat_Id.SelectedValue),
                    District_Id = long.Parse(ddlMDistrict_Id.SelectedValue),
                    PostCode = txtMPostCode.Text != "" ? int.Parse(txtMPostCode.Text) : 0,
                    Phone1 = txtMPhone1.Text,
                    Phone2 = txtMPhone2.Text,
                    Phone3 = txtMPhone3.Text,
                    InternalPhoneExtension = txtMInternalPhoneExtension.Text,
                    FAX = txtMFAX.Text,
                    Mobile1 = txtMMobile1.Text,
                    Mobile2 = txtMMobile2.Text,
                    Email1 = txtMEmail1.Text,
                    Email2 = txtMEmail2.Text,
                }

          );
            }

            else
            {
                myupdateworkaddressid = WorkAddress.SingleByFatherId(mymotherid).Id;

                WorkAddress.Update(new SD_WorkAddress
                {
                    Id = myupdateworkaddressid,

                    Career = ddlMCareer.SelectedValue,
                    CompanyName = txtMCompanyName.Text,
                    BuildingNo = txtMBuildingNo.Text != "" ? int.Parse(txtMBuildingNo.Text) : 0,
                    FloorNo = txtMFloorNo.Text != "" ? int.Parse(txtMFloorNo.Text) : 0,
                    FlatNo = txtMFlatNo.Text != "" ? int.Parse(txtMFlatNo.Text) : 0,
                    StreetName = txtMStreetName.Text,
                    Stat_Id = long.Parse(ddlMStat_Id.SelectedValue),
                    District_Id = long.Parse(ddlMDistrict_Id.SelectedValue),
                    PostCode = txtMPostCode.Text != "" ? int.Parse(txtMPostCode.Text) : 0,
                    Phone1 = txtMPhone1.Text,
                    Phone2 = txtMPhone2.Text,
                    Phone3 = txtMPhone3.Text,
                    InternalPhoneExtension = txtMInternalPhoneExtension.Text,
                    FAX = txtMFAX.Text,
                    Mobile1 = txtMMobile1.Text,
                    Mobile2 = txtMMobile2.Text,
                    Email1 = txtMEmail1.Text,
                    Email2 = txtMEmail2.Text,
                }

                  );
            }


            SD_Mother myupdatemother = new SD_Mother();

            myupdatemother.Id = mymotherid;
            myupdatemother.Student_Id = long.Parse(lblstudentid.Text);
            myupdatemother.Ar_FirstName = txtMAr_FirstName.Text;
            myupdatemother.Ar_SecondName = txtMAr_SecondName.Text;
            myupdatemother.Ar_ThirdName = txtMAr_ThirdNmae.Text;
            myupdatemother.Ar_FourthName = txtMAr_FourthName.Text;
            myupdatemother.En_FirstName = txtMEn_FirstName.Text;
            myupdatemother.En_SecondName = txtMEn_SecondName.Text;
            myupdatemother.En_ThirdName = txtMEn_ThirdName.Text;
            myupdatemother.En_FourthName = txtMEn_FourthName.Text;
            myupdatemother.UserName = txtMLoginName.Text;
            myupdatemother.Password = txtMLoginPassword.Text;

            if (txtMGregorian_BirthDate.Text.Length != 0)
            {
                myupdatemother.Gregorian_BirthDate = txtMGregorian_BirthDate.Text;
            }

            if (txtMHijri_BirthDate.Text.Length != 0)
            {
                myupdatemother.Hijri_BirthDate = txtMHijri_BirthDate.Text;
            }

            myupdatemother.Identification_Number = txtMIdentification_Number.Text;
            myupdatemother.Pasport_Number = txtMPasport_Number.Text;
            myupdatemother.Qualification_Id = long.Parse(ddlMQualification_Id.SelectedValue);
            myupdatemother.Specialization = txtMSpecialization.Text;
            myupdatemother.PointOfGraduation = txtMPointOfGraduation.Text;
            //if (txtMGreggorian_GraduationDate.Text.Length != 0)
            //{
            myupdatemother.Greggorian_GraduationDate = ddlMGreggorian_GraduationDate.SelectedItem.Text;
            //}

            //if (txtMHijri_GraduationDate.Text.Length != 0)
            //{
                myupdatemother.Hijri_GraduationDate =ddlMHijri_GraduationDate.SelectedItem.Text;
            //}

            myupdatemother.HomeAddress_Id = myupdatehomeaddressid;
            myupdatemother.WorkAddress_Id = myupdateworkaddressid;
            myupdatemother.Nationality_Id = long.Parse(lbMCountry_Id.SelectedValue);

            Mother.Update(myupdatemother);

        }


        //handel events on custody 
        protected void handelcustody(long mystudentid)
        {
            SD_Custody mycustody = Custody.SingleByStudentId(mystudentid);
            if (mycustody == null)
            {
                if (txtCAr_FirstName.Text.ToString().Length > 0)
                {
                    AddCustodyToStudent();
                    lblmessages.Text = "";
                }
            }

            else
            {
                long mycustodyid = mycustody.Id;
                updatecustodwithstudent(mycustodyid);
            }


        }

        // add new custody to curant student
        protected void AddCustodyToStudent()
        {
            if (chboxTheSameOfFatherAddress.Checked == true)
            {
                var newhomaddress = new SD_HomeAddress()

                {

                    BuildingNo = txtFhBuildingNo.Text != "" ? int.Parse(txtFhBuildingNo.Text) : 0,
                    FloorNo = txtFhFloorNo.Text != "" ? int.Parse(txtFhFloorNo.Text) : 0,
                    FlatNo = txtFhFlatNo.Text != "" ? int.Parse(txtFhFlatNo.Text) : 0,
                    StreetName = txtFhStreetName.Text,
                    PostCode = txtFhPostCode.Text != "" ? int.Parse(txtFhPostCode.Text) : 0,
                    Phone1 = txtFhPhone1.Text,
                    Phone2 = txtFhPhone2.Text,
                    Phone3 = txtFhPhone3.Text,                   

                };

                if (ddlFhStat_Id.SelectedValue != "0")
                {
                    newhomaddress.Stat_Id = long.Parse(ddlFhStat_Id.SelectedValue);

                    if (ddlFhDistrict_Id.SelectedValue != "0")
                        newhomaddress.District_Id = long.Parse(ddlFhDistrict_Id.SelectedValue);
                }
                myhomeaddressid = HomeAddress.Add(newhomaddress);
            }

            else
            {

                var newhomaddress = new SD_HomeAddress()

               {

                   BuildingNo = txtChBuildingNo.Text != "" ? int.Parse(txtChBuildingNo.Text) : 0,
                   FloorNo = txtChFloorNo.Text != "" ? int.Parse(txtChFloorNo.Text) : 0,
                   FlatNo = txtChFlatNo.Text != "" ? int.Parse(txtChFlatNo.Text) : 0,
                   StreetName = txtChStreetName.Text,
                   PostCode = txtChPostCode.Text != "" ? int.Parse(txtChPostCode.Text) : 0,
                   Phone1 = txtChPhone1.Text,
                   Phone2 = txtChPhone2.Text,
                   Phone3 = txtChPhone3.Text,

               };

                if (ddlChStat_Id.SelectedValue != "0")
                {
                    newhomaddress.Stat_Id = long.Parse(ddlChStat_Id.SelectedValue);

                    if (ddlChDistrict_Id.SelectedValue != "0")
                        newhomaddress.District_Id = long.Parse(ddlChDistrict_Id.SelectedValue);
                }
                myhomeaddressid = HomeAddress.Add(newhomaddress);

            }

            //*****************************************************************************************************************************
            var newworkaddress = new SD_WorkAddress()

           {
               Career = ddlFCareer.SelectedValue,
               CompanyName = txtCCompanyName.Text,
               BuildingNo = txtCBuildingNo.Text != "" ? int.Parse(txtCBuildingNo.Text) : 0,
               FloorNo = txtCFloorNo.Text != "" ? int.Parse(txtCFloorNo.Text) : 0,
               FlatNo = txtCFlatNo.Text != "" ? int.Parse(txtCFlatNo.Text) : 0,
               StreetName = txtCStreetName.Text,
               PostCode = txtCPostCode.Text != "" ? int.Parse(txtCPostCode.Text) : 0,
               Phone1 = txtCPhone1.Text,
               Phone2 = txtCPhone2.Text,
               Phone3 = txtCPhone3.Text,
               InternalPhoneExtension = txtCInternalPhoneExtension.Text,
               FAX = txtCFAX.Text,
               Mobile1 = txtCMobile1.Text,
               Mobile2 = txtCMobile2.Text,
               Email1 = txtCEmail1.Text,
               Email2 = txtCEmail2.Text,
           };

            if (ddlCStat_Id.SelectedValue != "0")
            {
                newworkaddress.Stat_Id = long.Parse(ddlCStat_Id.SelectedValue);
                if (ddlCDistrict_Id.SelectedValue != "0")
                    newworkaddress.District_Id = long.Parse(ddlCDistrict_Id.SelectedValue);
            }
            myworkaddressid = WorkAddress.Add(newworkaddress);



            if (myhomeaddressid == 0)
            {
                myhomeaddressid = null;
            }

            if (myworkaddressid == 0)
            {
                myworkaddressid = null;
            }



            var newcustody = new SD_Custody()

            {
                Student_Id = long.Parse(lblstudentid.Text),

                RelationToStudent = txtCRelationToStudent.Text,
                ParentsSocialStatus_Id = Convert.ToInt16(lbParentsSocialStatus_Id.SelectedValue),
                CustodyType_Id = Convert.ToInt16(lbCustodyType_Id.SelectedValue),
                StudentLivingPlace_Id = Convert.ToInt16(lbStudentLivingPlace_Id.SelectedValue),
                Ar_FirstName = txtCAr_FirstName.Text,
                Ar_SecondName = txtCAr_SecondName.Text,
                Ar_ThirdName = txtCAr_ThirdNmae.Text,
                Ar_FourthName = txtCAr_FourthName.Text,
                En_FirstName = txtCEn_FirstName.Text,
                En_SecondName = txtCEn_SecondName.Text,
                En_ThirdName = txtCEn_ThirdName.Text,
                En_FourthName = txtCEn_FourthName.Text,
                Identification_Number = txtCIdentification_Number.Text,
                Pasport_Number = txtCPasport_Number.Text,
                Qualification_Id = Int64.Parse(ddlCQualification_Id.Text),
                Specialization = txtCSpecialization.Text,
                PointOfGraduation = txtCPointOfGraduation.Text,
                HomeAddress_Id = myhomeaddressid,
                WorkAddress_Id = myworkaddressid,
                Nationality_Id = long.Parse(lbCCountry_Id.SelectedValue),
            };

            if (txtCGregorian_BirthDate.Text.Length != 0)
                newcustody.Gregorian_BirthDate =txtCGregorian_BirthDate.Text;

            if (txtCHijri_BirthDate.Text.Length != 0)
                newcustody.Hijri_BirthDate = txtCHijri_BirthDate.Text;

            //if (txtCGreggorian_GraduationDate.Text.Length != 0)
                newcustody.Greggorian_GraduationDate = ddlCGreggorian_GraduationDate.SelectedItem.Text;


            //if (txtCHijri_GraduationDate.Text.Length != 0)
               newcustody.Hijri_GraduationDate = ddlCHijri_GraduationDate.SelectedItem.Text;

            Custody.Add(newcustody);
        }

        //update exist custody on curant student 
        protected void updatecustodwithstudent(long mycustodyid)
        {
            long myupdatehomeaddressid;


            if (chboxTheSameOfFatherAddress.Checked == true)
            {


                if (HomeAddress.SingleBycustodyId(mycustodyid) == null)
                {
                    var newhomeaddress = new SD_HomeAddress()

                    {

                        BuildingNo = txtFhBuildingNo.Text != "" ? int.Parse(txtFhBuildingNo.Text) : 0,
                        FloorNo = txtFhFloorNo.Text != "" ? int.Parse(txtFhFloorNo.Text) : 0,
                        FlatNo = txtFhFlatNo.Text != "" ? int.Parse(txtFhFlatNo.Text) : 0,
                        StreetName = txtFhStreetName.Text,
                        PostCode = txtFhPostCode.Text != "" ? int.Parse(txtFhPostCode.Text) : 0,
                        Phone1 = txtFhPhone1.Text,
                        Phone2 = txtFhPhone2.Text,
                        Phone3 = txtFhPhone3.Text,

                    };
                    if (ddlFhStat_Id.SelectedValue != "0")
                    {
                        newhomeaddress.Stat_Id = long.Parse(ddlFhStat_Id.SelectedValue);
                        if (ddlFhDistrict_Id.SelectedValue != "0")
                            newhomeaddress.District_Id = long.Parse(ddlFhDistrict_Id.SelectedValue);
                    }
                    myupdatehomeaddressid = HomeAddress.Add(newhomeaddress);


                }

                else
                {
                    myupdatehomeaddressid = HomeAddress.SingleBycustodyId(mycustodyid).Id;

                    var newupdatehomeaddress = new SD_HomeAddress
                    {

                        Id = myupdatehomeaddressid,

                        BuildingNo = txtFhBuildingNo.Text != "" ? int.Parse(txtFhBuildingNo.Text) : 0,
                        FloorNo = txtFhFloorNo.Text != "" ? int.Parse(txtFhFloorNo.Text) : 0,
                        FlatNo = txtFhFlatNo.Text != "" ? int.Parse(txtFhFlatNo.Text) : 0,
                        StreetName = txtFhStreetName.Text,
                        PostCode = txtFhPostCode.Text != "" ? int.Parse(txtFhPostCode.Text) : 0,
                        Phone1 = txtFhPhone1.Text,
                        Phone2 = txtFhPhone2.Text,
                        Phone3 = txtFhPhone3.Text,

                    };

                    if (ddlFhStat_Id.SelectedValue != "0")
                    {
                        newupdatehomeaddress.Stat_Id = long.Parse(ddlFhStat_Id.SelectedValue);
                        if (ddlFhDistrict_Id.SelectedValue != "0")
                            newupdatehomeaddress.District_Id = long.Parse(ddlFhDistrict_Id.SelectedValue);
                    }

                    HomeAddress.Update(newupdatehomeaddress);


                }
            }

            else
            {


                if (HomeAddress.SingleBycustodyId(mycustodyid) == null)
                {
                    var newhomeaddress = new SD_HomeAddress()

                    {

                        BuildingNo = txtChBuildingNo.Text != "" ? int.Parse(txtChBuildingNo.Text) : 0,
                        FloorNo = txtChFloorNo.Text != "" ? int.Parse(txtChFloorNo.Text) : 0,
                        FlatNo = txtChFlatNo.Text != "" ? int.Parse(txtChFlatNo.Text) : 0,
                        StreetName = txtChStreetName.Text,
                        PostCode = txtChPostCode.Text != "" ? int.Parse(txtChPostCode.Text) : 0,
                        Phone1 = txtChPhone1.Text,
                        Phone2 = txtChPhone2.Text,
                        Phone3 = txtChPhone3.Text,

                    };
                    if (ddlChStat_Id.SelectedValue != "0")
                    {
                        newhomeaddress.Stat_Id = long.Parse(ddlChStat_Id.SelectedValue);
                        if (ddlChDistrict_Id.SelectedValue != "0")
                            newhomeaddress.District_Id = long.Parse(ddlChDistrict_Id.SelectedValue);
                    }
                    myupdatehomeaddressid = HomeAddress.Add(newhomeaddress);


                }

                else
                {
                    myupdatehomeaddressid = HomeAddress.SingleBycustodyId(mycustodyid).Id;

                    var newupdatehomeaddress = new SD_HomeAddress
                    {

                        Id = myupdatehomeaddressid,

                        BuildingNo = txtChBuildingNo.Text != "" ? int.Parse(txtChBuildingNo.Text) : 0,
                        FloorNo = txtChFloorNo.Text != "" ? int.Parse(txtChFloorNo.Text) : 0,
                        FlatNo = txtChFlatNo.Text != "" ? int.Parse(txtChFlatNo.Text) : 0,
                        StreetName = txtChStreetName.Text,

                        PostCode = txtChPostCode.Text != "" ? int.Parse(txtChPostCode.Text) : 0,
                        Phone1 = txtChPhone1.Text,
                        Phone2 = txtChPhone2.Text,
                        Phone3 = txtChPhone3.Text,

                    };

                    if (ddlChStat_Id.SelectedValue != "0")
                    {
                        newupdatehomeaddress.Stat_Id = long.Parse(ddlChStat_Id.SelectedValue);
                        if (ddlChDistrict_Id.SelectedValue != "0")
                            newupdatehomeaddress.District_Id = long.Parse(ddlChDistrict_Id.SelectedValue);
                    }
                    HomeAddress.Update(newupdatehomeaddress);


                }
            }
            // add work addreess to custody 
            long myupdateworkaddressid;
            if (WorkAddress.SingleByFatherId(mycustodyid) == null)
            {
                var newworkaddress = new SD_WorkAddress()

                {

                    Career = ddlFCareer.SelectedValue,
                    CompanyName = txtCCompanyName.Text,
                    BuildingNo = txtCBuildingNo.Text != "" ? int.Parse(txtCBuildingNo.Text) : 0,
                    FloorNo = txtCFloorNo.Text != "" ? int.Parse(txtCFloorNo.Text) : 0,
                    FlatNo = txtCFlatNo.Text != "" ? int.Parse(txtCFlatNo.Text) : 0,
                    StreetName = txtCStreetName.Text,

                    PostCode = txtCPostCode.Text != "" ? int.Parse(txtCPostCode.Text) : 0,
                    Phone1 = txtCPhone1.Text,
                    Phone2 = txtCPhone2.Text,
                    Phone3 = txtCPhone3.Text,
                    InternalPhoneExtension = txtCInternalPhoneExtension.Text,
                    FAX = txtCFAX.Text,
                    Mobile1 = txtCMobile1.Text,
                    Mobile2 = txtCMobile2.Text,
                    Email1 = txtCEmail1.Text,
                    Email2 = txtCEmail2.Text,
                };

                if (ddlCStat_Id.SelectedValue != "0")
                {
                    newworkaddress.Stat_Id = int.Parse(ddlCStat_Id.SelectedValue);
                    if (ddlCDistrict_Id.SelectedValue != "0")
                        newworkaddress.District_Id = int.Parse(ddlCDistrict_Id.SelectedValue);
                }
                myupdateworkaddressid = WorkAddress.Add(newworkaddress);


            }

            else
            {
                myupdateworkaddressid = WorkAddress.SingleByFatherId(mycustodyid).Id;

                var newupdateworkaddress = new SD_WorkAddress
                {
                    Id = myupdateworkaddressid,


                    Career = ddlFCareer.SelectedValue,
                    CompanyName = txtCCompanyName.Text,
                    BuildingNo = txtCBuildingNo.Text != "" ? int.Parse(txtCBuildingNo.Text) : 0,
                    FloorNo = txtCFloorNo.Text != "" ? int.Parse(txtCFloorNo.Text) : 0,
                    FlatNo = txtCFlatNo.Text != "" ? int.Parse(txtCFlatNo.Text) : 0,
                    StreetName = txtCStreetName.Text,

                    PostCode = txtCPostCode.Text != "" ? int.Parse(txtCPostCode.Text) : 0,
                    Phone1 = txtCPhone1.Text,
                    Phone2 = txtCPhone2.Text,
                    Phone3 = txtCPhone3.Text,
                    InternalPhoneExtension = txtCInternalPhoneExtension.Text,
                    FAX = txtCFAX.Text,
                    Mobile1 = txtCMobile1.Text,
                    Mobile2 = txtCMobile2.Text,
                    Email1 = txtCEmail1.Text,
                    Email2 = txtCEmail2.Text,
                };

                if (ddlCStat_Id.SelectedValue != "0")
                {
                    newupdateworkaddress.Stat_Id = int.Parse(ddlCStat_Id.SelectedValue);
                    if (ddlCDistrict_Id.SelectedValue != "0")
                        newupdateworkaddress.District_Id = int.Parse(ddlCDistrict_Id.SelectedValue);
                }
                WorkAddress.Update(newupdateworkaddress);


            }


            SD_Custody myupdatecustody = new SD_Custody();

            myupdatecustody.Id = mycustodyid;

            myupdatecustody.Student_Id = long.Parse(lblstudentid.Text);
            myupdatecustody.RelationToStudent = txtCRelationToStudent.Text;
            myupdatecustody.ParentsSocialStatus_Id = int.Parse(lbParentsSocialStatus_Id.SelectedValue);
            myupdatecustody.CustodyType_Id = int.Parse(lbCustodyType_Id.SelectedValue);
            myupdatecustody.StudentLivingPlace_Id = int.Parse(lbStudentLivingPlace_Id.SelectedValue);
            myupdatecustody.Ar_FirstName = txtCAr_FirstName.Text;
            myupdatecustody.Ar_SecondName = txtCAr_SecondName.Text;
            myupdatecustody.Ar_ThirdName = txtCAr_ThirdNmae.Text;
            myupdatecustody.Ar_FourthName = txtCAr_FourthName.Text;
            myupdatecustody.En_FirstName = txtCEn_FirstName.Text;
            myupdatecustody.En_SecondName = txtCEn_SecondName.Text;
            myupdatecustody.En_ThirdName = txtCEn_ThirdName.Text;
            myupdatecustody.En_FourthName = txtCEn_FourthName.Text;            

            if (txtCGregorian_BirthDate.Text.Length != 0)
            {
                myupdatecustody.Gregorian_BirthDate = txtCGregorian_BirthDate.Text;

            }

            if (txtCHijri_BirthDate.Text.Length != 0)
            {
                myupdatecustody.Hijri_BirthDate = txtCHijri_BirthDate.Text;
            }

            myupdatecustody.Identification_Number = txtCIdentification_Number.Text;
            myupdatecustody.Pasport_Number = txtCPasport_Number.Text;
            myupdatecustody.Qualification_Id = long.Parse(ddlCQualification_Id.SelectedValue);
            myupdatecustody.Specialization = txtCSpecialization.Text;
            myupdatecustody.PointOfGraduation = txtCPointOfGraduation.Text;

            //if (txtCGreggorian_GraduationDate.Text.Length != 0)
            //{
                myupdatecustody.Greggorian_GraduationDate = ddlCGreggorian_GraduationDate.SelectedItem .Text;

            //}

            //if (txtCHijri_GraduationDate.Text.Length != 0)
            //{
                myupdatecustody.Hijri_GraduationDate =ddlCHijri_GraduationDate.SelectedItem.Text;
            //}

            myupdatecustody.HomeAddress_Id = myupdatehomeaddressid;
            myupdatecustody.WorkAddress_Id = myupdateworkaddressid;
            myupdatecustody.Nationality_Id = long.Parse(lbCCountry_Id.SelectedValue);

            Custody.Update(myupdatecustody);

        }


        //handel events on xschool 
        protected void handelxschool(long mystudentid)
        {
            IEnumerable<SD_XSchool> myxschool = XSchool.SingleByStudentId(mystudentid);
            if (myxschool.Count() == 0)
            {
                AddXSchoolToStudent();
            }
            else
            {
                XSchool.Deletebystudentid(mystudentid);
                AddXSchoolToStudent();

            }



        }

        protected void AddXSchoolToStudent()
        {
            int j;
            for (j = 0; j <= griddataxschools.Rows.Count - 1; j++)
            {
                // long myStudent_Id = 1;
                long myStudent_Id = long.Parse(lblstudentid.Text);


                String mySchoolName = griddataxschools.Rows[j].Cells[3].Text;
                DateTime mySatrtDate = Convert.ToDateTime(griddataxschools.Rows[j].Cells[4].Text);
                DateTime myEndDate = Convert.ToDateTime(griddataxschools.Rows[j].Cells[5].Text);
                String myAddress = griddataxschools.Rows[j].Cells[6].Text;
                String myCertification = griddataxschools.Rows[j].Cells[7].Text;
                String myGrade = griddataxschools.Rows[j].Cells[8].Text;
                String myClass = griddataxschools.Rows[j].Cells[9].Text;
                String myFirstLanguage = griddataxschools.Rows[j].Cells[10].Text;
                String mySecondLanguage = griddataxschools.Rows[j].Cells[11].Text;

                XSchool.Add(new SD_XSchool()

                {
                    Student_Id = myStudent_Id,
                    SchoolName = mySchoolName,
                    SatrtDate = mySatrtDate,
                    EndDate = myEndDate,
                    Address = myAddress,
                    Certification = myCertification,
                    Grade = myGrade,
                    Class = myClass,
                    FirstLanguage = myFirstLanguage,
                    SecondLanguage = mySecondLanguage,

                }
                );

            }
        }

        //handel events on StudentQualificatioSkill 
        protected void handelStudentQualificatioSkill(long mystudentid)
        {
            IEnumerable<SD_StudentQualificatioSkill> myStudentQualificatioSkill = StudentQualificatioSkill.SingleByStudentId(mystudentid);
            if (myStudentQualificatioSkill.Count() == 0)
            {
                AddQualificationSkillesToStudent();
            }
            else
            {
                int j;
                for (j = 0; j <= QualificationSkillRepeater.Items.Count - 1; j++)
                {
                    Label lblid = default(Label);
                    lblid = QualificationSkillRepeater.Items[j].FindControl("lblID") as Label;
                    String id = "";
                    id = lblid.Text;

                    TextBox txtfdet = default(TextBox);
                    txtfdet = QualificationSkillRepeater.Items[j].FindControl("txtDetail") as TextBox;
                    String mydet = txtfdet.Text;

                    Boolean isexist;
                    CheckBox mycbIsExist = default(CheckBox);
                    mycbIsExist = QualificationSkillRepeater.Items[j].FindControl("cbIsExist") as CheckBox;
                    if (mycbIsExist.Checked)
                    {
                        txtfdet.ReadOnly = false;
                        isexist = true;
                    }
                    else
                    {
                        txtfdet.Text = "";
                        txtfdet.ReadOnly = true;

                        isexist = false;
                    }

                    StudentQualificatioSkill.Update(new SD_StudentQualificatioSkill()

                    {
                        Id = myStudentQualificatioSkill.ElementAt(j).Id,
                        Student_Id = long.Parse(lblstudentid.Text),
                        QualificationSkill_Id = long.Parse(id),
                        Detail = mydet,
                        IsExist = isexist,
                    }
                  );
                }
            }


        }

        protected void AddQualificationSkillesToStudent()
        {
            int j;
            for (j = 0; j <= QualificationSkillRepeater.Items.Count - 1; j++)
            {
                Label lblid = default(Label);
                lblid = QualificationSkillRepeater.Items[j].FindControl("lblID") as Label;
                String id = "";
                id = lblid.Text;

                TextBox txtfdet = default(TextBox);
                txtfdet = QualificationSkillRepeater.Items[j].FindControl("txtDetail") as TextBox;
                String mydet = txtfdet.Text;

                Boolean isexist;
                CheckBox mycbIsExist = default(CheckBox);
                mycbIsExist = QualificationSkillRepeater.Items[j].FindControl("cbIsExist") as CheckBox;
                if (mycbIsExist.Checked)
                {
                    txtfdet.ReadOnly = false;
                    isexist = true;
                }
                else
                {
                    txtfdet.Text = "";
                    txtfdet.ReadOnly = true;

                    isexist = false;
                }

                StudentQualificatioSkill.Add(new SD_StudentQualificatioSkill()

                {

                    Student_Id = long.Parse(lblstudentid.Text),
                    QualificationSkill_Id = long.Parse(id),
                    Detail = mydet,
                    IsExist = isexist,


                }
              );


                txtfdet.Text = "";
                mycbIsExist.Checked = false;

            }
        }

        //handel events on emergancy 
        protected void handelemergancy(long mystudentid)
        {

            SD_Emergency myemergancy = Emergency.SingleByStudentId(mystudentid);
            if (myemergancy == null)
            {
                AddEmergancyDataToStudent();
            }

            else
            {
                Emergency.Update(new SD_Emergency
                {
                    Emergency_Id = myemergancy.Emergency_Id,
                    Student_Id = long.Parse(lblstudentid.Text),
                    Name1 = txtName1.Text,
                    Name2 = txtName2.Text,
                    Name3 = txtName3.Text,
                    Name4 = txtName4.Text,
                    Name5 = txtName5.Text,

                    Phone1 = txtPhone1.Text,
                    Phone2 = txtPhone2.Text,
                    Phone3 = txtPhone3.Text,
                    Phone4 = txtPhone4.Text,
                    Phone5 = txtPhone5.Text,

                    MobilePhone1 = txtMobilePhone1.Text,
                    MobilePhone2 = txtMobilePhone2.Text,
                    MobilePhone3 = txtMobilePhone3.Text,
                    MobilePhone4 = txtMobilePhone4.Text,
                    MobilePhone5 = txtMobilePhone5.Text,

                });
            }

        }

        protected void AddEmergancyDataToStudent()
        {
            // long x = txtAddress.Text != "" ? long.Parse(txtAddress.Text) : 0;

            Emergency.Add(new SD_Emergency()

            {


                Student_Id = long.Parse(lblstudentid.Text),
                Name1 = txtName1.Text,
                Name2 = txtName2.Text,
                Name3 = txtName3.Text,
                Name4 = txtName4.Text,
                Name5 = txtName5.Text,

                Phone1 = txtPhone1.Text,
                Phone2 = txtPhone2.Text,
                Phone3 = txtPhone3.Text,
                Phone4 = txtPhone4.Text,
                Phone5 = txtPhone5.Text,

                MobilePhone1 = txtMobilePhone1.Text,
                MobilePhone2 = txtMobilePhone2.Text,
                MobilePhone3 = txtMobilePhone3.Text,
                MobilePhone4 = txtMobilePhone4.Text,
                MobilePhone5 = txtMobilePhone5.Text,


            }
            );

        }

        //handel events on relative 
        protected void handelrelative(long mystudentid)
        {
            IEnumerable<TE_Student_Relatives> mystudentrelative = StudentRelatives.SingleByStudentId(mystudentid);
            if (mystudentrelative.Count() == 0)
            {
                AddRelativeToStudent();
            }
            else
            {
                long mystid = long.Parse(lblstudentid.Text);
                int idloop;
                for (idloop = 0; idloop <= mystudentrelative.Count() - 1; idloop++)
                {
                    StudentRelatives.DeletebyStudentId(mystid);
                }
                int j;
                for (j = 0; j <= griddatarelative.Rows.Count - 1; j++)
                {
                    // long myStudent_Id = griddatarelative.Rows[j].Cells[1].Text != "" ? int.Parse(griddatarelative.Rows[j].Cells[1].Text) : 0;
                    long myStudent_Id = long.Parse(lblstudentid.Text);
                    long myRelativeStudent_Id = long.Parse(griddatarelative.Rows[j].Cells[3].Text);
                    String myDetails = griddatarelative.Rows[j].Cells[5].Text;
                    StudentRelatives.Add(new TE_Student_Relatives()
                    {
                        Student_Id = myStudent_Id,
                        Relative_student_Id = myRelativeStudent_Id,
                        Details = myDetails,
                    }
                               );
                }
            }


        }

        protected void AddRelativeToStudent()
        {


            int j;
            for (j = 0; j <= griddatarelative.Rows.Count - 1; j++)
            {
                // long myStudent_Id = griddatarelative.Rows[j].Cells[1].Text != "" ? int.Parse(griddatarelative.Rows[j].Cells[1].Text) : 0;
                long myStudent_Id = long.Parse(lblstudentid.Text);
                long myRelativeStudent_Id = long.Parse(griddatarelative.Rows[j].Cells[3].Text);
                String myDetails = griddatarelative.Rows[j].Cells[5].Text;

                StudentRelatives.Add(new TE_Student_Relatives()

                {
                    Student_Id = myStudent_Id,
                    Relative_student_Id = myRelativeStudent_Id,
                    Details = myDetails,


                }
                );

            }



        }


        protected void ddlCampus_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlCampus.SelectedValue != "0")
            {
                ddlCertificate.Items.Clear();
                ddlStadge.Items.Clear();
                ddltopgrad.Items.Clear();

                ddlCertificate.DataSource = Structure.GetCertificatesByCampus(long.Parse(ddlCampus.SelectedValue));
                ddlCertificate.DataTextField = "Name";
                ddlCertificate.DataValueField = "Id";
                ddlCertificate.DataBind();
                ddlCertificate.Items.Insert(0, new ListItem("اختيار", "-1"));

                ddlCertificate.Enabled = true;
                ddlStadge.Enabled = false;
                ddltopgrad.Enabled = false;

            }

            else
            {
                ddlCertificate.Items.Clear();
                ddlStadge.Items.Clear();
                ddltopgrad.Items.Clear();
                ddlCertificate.Enabled = false;
                ddlStadge.Enabled = false;
                ddltopgrad.Enabled = false;

            }
        }

        protected void ddlCertificate_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlCertificate.SelectedValue != "0")
            {
                ddlStadge.Items.Clear();
                ddltopgrad.Items.Clear();

                ddlStadge.DataSource = Structure.GetStagesByCertificate(long.Parse(ddlCertificate.SelectedValue));
                ddlStadge.DataTextField = "Name";
                ddlStadge.DataValueField = "Id";
                ddlStadge.DataBind();
                ddlStadge.Items.Insert(0, new ListItem("اختيار", "-1"));

                ddlStadge.Enabled = true;
                ddltopgrad.Enabled = false;

            }

            else
            {
                ddlStadge.Items.Clear();
                ddltopgrad.Items.Clear();

                ddlStadge.Enabled = false;
                ddltopgrad.Enabled = false;
            }
        }

        protected void ddlStadge_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlStadge.SelectedValue != "0")
            {
                ddltopgrad.Items.Clear();
                ddltopgrad.DataSource = Structure.GetGradesByStage(long.Parse(ddlStadge.SelectedValue));
                ddltopgrad.DataTextField = "Name";
                ddltopgrad.DataValueField = "Id";
                ddltopgrad.DataBind();
                ddltopgrad.Items.Insert(0, new ListItem("اختيار", "-1"));

                ddltopgrad.Enabled = true;

            }

            else
            {
                ddltopgrad.Items.Clear();
                ddltopgrad.Enabled = false;


            }
        }

        protected void ddlCertification_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlCertification.SelectedValue != "0")
            {
                ddlGrade.Items.Clear();
                ddlClass.Items.Clear();
                ddlGrade.DataSource = Structure.GetStagesByCertificate(long.Parse(ddlCertification.SelectedValue));
                ddlGrade.DataTextField = "Name";
                ddlGrade.DataValueField = "Id";
                ddlGrade.DataBind();
                ddlGrade.Items.Insert(0, new ListItem("اختيار", "-1"));

                ddlGrade.Enabled = true;
                ddlClass.Enabled = false;
            }

            else
            {

                ddlGrade.Items.Clear();
                ddlGrade.Enabled = false;
                ddlClass.Items.Clear();
                ddlClass.Enabled = false;
            }


        }

        protected void ddlGrade_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlGrade.SelectedValue != "0")
            {
                ddlClass.Items.Clear();
                ddlClass.DataSource = Structure.GetGradesByStage(long.Parse(ddlGrade.SelectedValue));
                ddlClass.DataTextField = "Name";
                ddlClass.DataValueField = "Id";
                ddlClass.DataBind();
                ddlClass.Items.Insert(0, new ListItem("اختيار", "-1"));


                ddlClass.Enabled = true;
            }

            else
            {
                ddlClass.Items.Clear();
                ddlClass.Enabled = false;

            }

        }

        protected void chekboxthesameaddreeforfather_CheckedChanged(object sender, EventArgs e)
        {
            if (chekboxthesameaddreeforfather.Checked)
            {
                pnlsamefatheraddress.Visible = false;

            }

            else
            {
                pnlsamefatheraddress.Visible = true;
            }

        }

        protected void chekboxmother_CheckedChanged(object sender, EventArgs e)
        {
            if (chekboxmother.Checked)
            {

                pnlhouswaif.Visible = false;
            }

            else
            {
                pnlhouswaif.Visible = true;
            }
        }

        protected void typeid_CheckedChanged(object sender, EventArgs e)
        {
            if (typeid.Checked)
            {
                pnlcustodytype.Visible = false;
            }

            else
            {
                pnlcustodytype.Visible = true;
            }
        }

        protected void ddlFStat_Id_SelectedIndexChanged(object sender, EventArgs e)
        {


            if (ddlFStat_Id.SelectedIndex > 0)
            {

                ddlFDistrict_Id.Items.Clear();
                ddlFDistrict_Id.DataSource = LookUps.GetDestrictByState(int.Parse(ddlFStat_Id.SelectedValue));
                ddlFDistrict_Id.DataTextField = "Name";
                ddlFDistrict_Id.DataValueField = "Id";
                ddlFDistrict_Id.DataBind();
                ddlFDistrict_Id.Enabled = true;
            }

            else
            {
                ddlFDistrict_Id.Items.Clear();
                ddlFDistrict_Id.Enabled = false;


            }
        }

        protected void chboxTheSameOfFatherAddress_CheckedChanged(object sender, EventArgs e)
        {
            if (chboxTheSameOfFatherAddress.Checked)
            {

                PnlCustodySameOfFatherAddress.Visible = false;
            }

            else
            {
                PnlCustodySameOfFatherAddress.Visible = true;

            }
        }

        protected void ddlFhStat_Id_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (ddlFhStat_Id.SelectedValue != "0")
            {
                ddlFhDistrict_Id.Items.Clear();
                ddlFhDistrict_Id.DataSource = LookUps.GetDestrictByState(int.Parse(ddlFhStat_Id.SelectedValue));
                ddlFhDistrict_Id.DataTextField = "Name";
                ddlFhDistrict_Id.DataValueField = "Id";
                ddlFhDistrict_Id.DataBind();
                ddlFhDistrict_Id.Enabled = true;

            }
            else
            {
                ddlFhDistrict_Id.Items.Clear();
                ddlFhDistrict_Id.Enabled = false;

            }
        }

        protected void ddlMStat_Id_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (ddlMStat_Id.SelectedValue != "0")
            {
                ddlMDistrict_Id.Items.Clear();
                ddlMDistrict_Id.DataSource = LookUps.GetDestrictByState(int.Parse(ddlMStat_Id.SelectedValue));
                ddlMDistrict_Id.DataTextField = "Name";
                ddlMDistrict_Id.DataValueField = "Id";
                ddlMDistrict_Id.DataBind();
                ddlMDistrict_Id.Enabled = true;

            }

            else
            {
                ddlMDistrict_Id.Items.Clear();
                ddlMDistrict_Id.Enabled = false;


            }
        }

        protected void ddlMhStat_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlMhStat_Id.SelectedValue != "0")
            {
                ddlMhDistrict_Id.Items.Clear();
                ddlMhDistrict_Id.DataSource = LookUps.GetDestrictByState(int.Parse(ddlMhStat_Id.SelectedValue));
                ddlMhDistrict_Id.DataTextField = "Name";
                ddlMhDistrict_Id.DataValueField = "Id";
                ddlMhDistrict_Id.DataBind();
                ddlMhDistrict_Id.Enabled = true;

            }

            else
            {
                ddlMhDistrict_Id.Items.Clear();

                ddlMhDistrict_Id.Enabled = false;

            }
        }

        protected void ddlCStat_Id_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (ddlCStat_Id.SelectedValue != "0")
            {
                ddlCDistrict_Id.Items.Clear();
                ddlCDistrict_Id.DataSource = LookUps.GetDestrictByState(int.Parse(ddlCStat_Id.SelectedValue));
                ddlCDistrict_Id.DataTextField = "Name";
                ddlCDistrict_Id.DataValueField = "Id";
                ddlCDistrict_Id.DataBind();
                ddlCDistrict_Id.Enabled = true;
            }

            else
            {
                ddlCDistrict_Id.Items.Clear();
                ddlCDistrict_Id.Enabled = true;
            }

        }

        protected void ddlChStat_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlChStat_Id.SelectedValue != "0")
            {
                ddlChDistrict_Id.Items.Clear();
                ddlChDistrict_Id.DataSource = LookUps.GetDestrictByState(int.Parse(ddlChStat_Id.SelectedValue));
                ddlChDistrict_Id.DataTextField = "Name";
                ddlChDistrict_Id.DataValueField = "Id";
                ddlChDistrict_Id.DataBind();
                ddlChDistrict_Id.Enabled = true;
            }

            else
            {
                ddlChDistrict_Id.Items.Clear();
                ddlChDistrict_Id.Enabled = false;

            }

        }

        protected void btnsaverelative_Click(object sender, EventArgs e)
        {

            dtmystudent = new DataTable("relativestudent");

            DataColumn col = dtmystudent.Columns.Add("id", typeof(Int32));
            col.AllowDBNull = false;
            col.Unique = true;


            dtmystudent.Columns.Add("Student_Id", typeof(long));
            col.AllowDBNull = false;

            dtmystudent.Columns.Add("RelativeStudent_Id", typeof(long));
            col.AllowDBNull = false;

            dtmystudent.Columns.Add("اسم الطالب", typeof(String));

            dtmystudent.Columns.Add("نوع الصلة", typeof(String));


            DataRow row;

            if (txtmyrelstudentid.Text != "")
            {

                row = dtmystudent.NewRow();

                if (Session["dtmystudent"] != null)
                {
                    int x = 0;
                    for (i = 0; i <= ((DataTable)Session["dtmystudent"]).Rows.Count - 1; i++)
                    {

                        x = x + 1;

                    }

                    row["id"] = x + 1;
                }

                else
                {
                    row["id"] = 1;


                }

                row["Student_Id"] = lblstudentid.Text;
                row["RelativeStudent_Id"] = lblmyrelstudentid.Text;
                row["اسم الطالب"] = txtmyrelstudentid.Text;
                row["نوع الصلة"] = "اخ";
                dtmystudent.Rows.Add(row);
            }
            //********************************************

            if (Session["dtmystudent"] != null)
            {
                dtmystudent.Merge((DataTable)Session["dtmystudent"]);
            }
            Session["dtmystudent"] = dtmystudent;


            if (txtrelativeresualt.Text != "")
            {

                row = dtmystudent.NewRow();

                if (Session["dtmystudent"] != null)
                {
                    int x = 0;
                    for (i = 0; i <= ((DataTable)Session["dtmystudent"]).Rows.Count - 1; i++)
                    {

                        x = x + 1;

                    }

                    row["id"] = x + 1;
                }

                else
                {
                    row["id"] = 1;


                }
                row["Student_Id"] = lblstudentid.Text;
                row["RelativeStudent_Id"] = lblrelativeresualt.Text;
                row["اسم الطالب"] = txtrelativeresualt.Text;
                row["نوع الصلة"] = txtrelativedet.Text;
                dtmystudent.Rows.Add(row);



                Session["dtmystudent"] = dtmystudent;
            }

            griddatarelative.DataSource = dtmystudent;
            griddatarelative.DataBind();


            clearrelativegrid();
        }

        protected void clearrelativegrid()
        {
            txtmyrelstudentid.Text = "";
            lblmyrelstudentid.Text = "";
            txtrelativeresualt.Text = "";
            lblrelativeresualt.Text = "";
            txtrelativedet.Text = "";
        }

        protected void griddatarelative_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "OnDelete")
            {
                GridViewRow selectedRow = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
                DataKey rsid = griddatarelative.DataKeys[selectedRow.RowIndex];

                if (Session["dtmystudent"] != null)
                {
                    ((DataTable)Session["dtmystudent"]).Rows[selectedRow.RowIndex].Delete();

                }
            }
            griddatarelative.DataSource = Session["dtmystudent"];
            griddatarelative.DataBind();
        }

        protected void griddatarelative_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            e.Row.Cells[1].Visible = false;
            e.Row.Cells[2].Visible = false;
            e.Row.Cells[3].Visible = false;
        }

        protected void btnaddrelative_Click(object sender, EventArgs e)
        {

        }
        protected void updateImageUrl()
        {
            if (System.IO.File.Exists(Server.MapPath("~/StudentPics/") + lblstudentid.Text + ".jpg") == true)
            {
                Image1.ImageUrl = "~//StudentPics//" + lblstudentid.Text + ".jpg";
            }
        }

        
    }
}